I have used the answer to this question successfully but do not know how to also have the id of the selected item to post to the controller.

There is a comment made by @agarwaen implying that it is possible, but he doesn't say how.

I want to display the name in the dropdown, fill a textbox with the points associated to that item and post the id of the selected item to the controller.

I can currently do the first two, but it is posting the points back to the controller, not the id.

The ViewBag is being filled like this:

ViewBag.ActivityID = new SelectList(allowedActivities, "Points", "Name", "ActivityID");

*allowedActivities is the name of the list that holds the activity objects (which each have a name, id, and points)

And the javascript being used is:

$(document).ready(function () {
  $("#activityName").change(function () {
  var $this = $(this);
  var selectedValue = $this.val();
  $("#activityPoints").val(selectedValue);
  });
});

The razor is:

<div class="form-group">
   @Html.LabelFor(model => model.ActivityID, "Activity Name", htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
      @Html.DropDownList("ActivityID", null, "----Select An Activity----", htmlAttributes: new { @class = "form-control", @id = "activityName"})
   </div>
</div>

<div class="form-group">
   @Html.LabelFor(model => model.Activity.CPDTPoints, "CPTD Points", htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
      @Html.TextBoxFor(model => model.Activity.CPDTPoints, new { disabled = "disabled", @readonly = "readonly", @class = "form-control", @id = "activityPoints" })
    </div>
</div>

I want to populate the textbox with the points from the selected item in the dropdown (again, this is workings) but post the id to the controller (it is currently posting the points).

Related posts

Recent Viewed