How to save selected dropdownlist after postback

In asp.net mvc3 how to save a dropdown list of a selected item after postback.

+4
source share
6 answers

Do something like this:

[HttpPost] public ActionResult Create(FormCollection collection) { if (TryUpdateModel(yourmodel)) { //your logic return RedirectToAction("Index"); } int selectedvalue = Convert.ToInt32(collection["selectedValue"]); ViewData["dropdownlist"] = new SelectList(getAllEvents.ToList(), "EventID", "Name", selectedvalue);// your dropdownlist return View(); } 

And in the view:

  <%: Html.DropDownListFor(model => model.ProductID, (SelectList)ViewData["dropdownlist"])%> 
+2
source

Even easier, you can specify the name (s) of your dropdown lists in your ActionResult input parameters. Your drop-down lists should be in the form of tags. When an ActionResult is sent to the site, ASP.Net will iterate through requests, form values, and cookies. As long as you include the names of the drop-down list, the selected values ​​will be saved.

Here I have a form with three dropdowns that fit in an ActionResult. Drop-down (case insensitive) names: ReportName, Year, and Month.

  //MAKE SURE TO ACCEPT THE VALUES FOR REPORTNAME, YEAR, AND MONTH SO THAT THEY PERSIST IN THE DROPDOWNS EVEN AFTER POST!!!! [AcceptVerbs(HttpVerbs.Post)] public ActionResult ReportSelection(string reportName, string year, string month) { PopulateFilterDrowdowns(); return View("NameOfMyView"); } 
+2
source

MVC does not use ViewState, which means that you will need to manage the persistence of the value yourself. This is usually done using your model. So, given that you have a view model, for example:

 public class MyViewModel { } 

And your controller:

 public class MyController : Controller { public ActionResult Something() { return View(new MyViewModel()); } public ActionResult Something(MyViewModel model) { if (!ModelState.IsValid) return View(model); return RedirectToAction("Index"); } } 

Now, when you pass the model back to the data view (possibly wrong - the validation fails), when you use your DropDownListFor method, just pass the value:

 @Model.DropDownListFor(m => m.Whatever, new SelectList(...)) 

... etc.

Model Binding MVC will take care of reading the data in your model, you just need to make sure that you pass this back to the view in order to display the same value again.

+1
source

Assuming the selected item is part of the message, the controller now knows what it is. Just enter an entry in the ViewData dictionary indicating which item should be selected (null on get or if nothing was selected). In the view, check the value, and if it is not null, select the appropriate option.

0
source

Use an HttpRequestBase object. According to this, this should work:

  @Html.DropDownList("mydropdown", ViewBag.Itens as IEnumerable<SelectListItem>, new { value = Request["mydropdown"] }) 
0
source

If you create a data source with a drop-down list in the controller action method, you can send it the selected value

Controller:

  public ActionResult Index( int serviceid=0) { // build the drop down list data source List<Service> services = db.Service.ToList(); services.Insert(0, new Service() { ServiceID = 0, ServiceName = "All" }); // serviceid is the selected value you want to maintain ViewBag.ServicesList = new SelectList(services, "ServiceID", "ServiceName",serviceid); if (serviceid == 0) { //do something } else { // do another thing } return View(); } 

View:

  //ServiceList is coming from ViewBag @Html.DropDownList("ServicesList", null, htmlAttributes: new { @class = "form-control" }) 
0
source

All Articles