0

In my MVC app, I need to update the values in a DropDownList after a post.

My view:

 @{
 IEnumerable<MyModel.MySelectItem> mylist= ViewBag.MyList;
 }

 @using (Ajax.BeginForm(...........)
 {
 @Html.DropDownList("myselection", mylist.ToSelectList(p => p.Description, p => p.Description), "Select Item")
  ....Other controls and a submit button are here...
 }

My controller:

 //Populate list in Index()
 ViewBag.MyList = myGeneratedList;
 return View();

When the view is initially displayed, the DropDownList is populated with the correct values.

Here is the post method:

public ActionResult GetData()
{
 ActionResult result = null;     

//Query data...
ViewBag.MyList = myNEWGeneratedList;

//Need to display a table of results and update DropDownList
var myTableResults = GetSomeData();
result = PartialView("_MyTableResultsView", myTableResults);
return results; 
}

When the form posts back, the partial view displays it's results fine, but the DropDownList does NOT get updated. How can I get the DropDownList in the view to update after a post?

1 Answer 1

0

Ajax.beginform will update a specific control based on the value of the UpdateTargetId parameter. If your dropdownlist is not within the target control, it will not be changed. If it is in the targeted control, it will be updated or overwritten with whatever is returned in your partial view.

If you want your dropdownlist to update outside of the returned partial view or UpdateTarget, you would most likely use javascript or jquery to update it client side.

Another option is to use HTML.beginform which will issue a full page postback (refresh everything), but your controller would need to be updated to return a view that contains the updated dropdownlist as well as the code in your current partial view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.