How to keep dropdownlist selected value after postback

后端 未结 6 1176
日久生厌
日久生厌 2020-12-19 13:03

In asp.net mvc3 how to keep dropdown list selected item after postback.

6条回答
  •  [愿得一人]
    2020-12-19 13:20

    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"])%>
    

提交回复
热议问题