user1019480
user1019480

Reputation: 3

maintain state of dropdownlist in MVC3

How do I maintain the selected value of dropdownlist in MVC3?

I'm using the following code to create the drop down list:

<%= Html.DropDownList("PEDropDown", 
        (IEnumerable<SelectListItem>)ViewData["PEDropDown"], 
        new { onchange = "this.form.action='/Screener/Screener';this.form.submit();" }
)%>

Upvotes: 0

Views: 1684

Answers (4)

The Godfather
The Godfather

Reputation: 929

You may try this. Using ViewBag instead of ViewData (I suggest, it is better to use Model object)

Html.DropDownList("PEDropDown", new SelectList(ViewBag.PEDropDown, "Key", "Value", Model.PEDropDownSelectedValue), new { onchange = "document.location.href = '/ControllerName/ActionMethod?selectedValue=' + this.options[this.selectedIndex].value;" }))

The fourth argument in SelectList is the selected value. It must be passed using the model object. When you call the particular action method, set the model object as below.

public ActionResult ActionMethod(string selectedValue)
{
    ViewModelPE objModel = new ViewModelPE();
    // populate the dropdown, since you lost the list in Viewbag
    ViewBag.PEDropDown = functionReturningListPEDropDown();
    objModel.PEDropDownSelectedValue = selectedValue;
    return View(objModel);
    // You may use the model object to pass the list too instead of ViewBag (ViewData in your case)
}

Upvotes: 0

DevDave
DevDave

Reputation: 6908

pass the value back into your controller and then populate a List of SelectListItems in the controller:

public actionresult yourmethod (int idToPass)
{

List<SelectListItem> SLIList = new List<SelectListItem>();
foreach (Model model in dropdownList)
{
   SelectListItem SLI = new SelectListItem();
   SLI.text = model.CategoryName;
   SLI.selected = model.CategoryId == idToPass;
   SLIList.Add(SLI);
}
   ViewData["myDDL"] = SLIList;
}

Upvotes: 0

Prasanth
Prasanth

Reputation: 3041

Here is one example, I use. I am not sure, this is the way you use to populate the DropDownList

<%=Html.DropDownList("ddlCategories", IEnumerable<SelectListItem>)ViewData["PEDropDown"], "CategoryId", "CategoryName", Model.CategoryId), "Select Category", new { onchange = "this.form.action='/Screener/Screener';this.form.submit();"})%>

Another way is, make a select list in controller as follows

List<SelectListItem> CategoryList = new List<SelectListItem>();
                foreach (var item in Categories)
                {

                    CategoryList.Add(new SelectListItem
                    {
                        Selected = Model.CategoryId, 
                        Text = item.CategoryName, Value = Convert.ToString(item.CategoryId) });
                }
ViewData["PEDropDown"]=CategoryList;

and use in view as

<%:Html.DropDownList("ddlCategories",IEnumerable<SelectListItem>)ViewData["PEDropDown"], "CategoryId", "CategoryName", new { onchange = "this.form.action='/Screener/Screener';this.form.submit();"})%>

Upvotes: 1

Ron Sijm
Ron Sijm

Reputation: 8758

I'm not 100% sure I get what you want to do, but I assume you want to get the selected value from the dropdown list?

In that case:

new { onchange = "alert(this.options[this.selectedIndex].value);" }

I put it in an alert for now, because I don't know what you want to do with the value

Upvotes: 0

Related Questions