HTML attribute for Html.Dropdown

I use the dropdown as follows.

<%=Html.DropDownList("ddl", ViewData["Available"] as SelectList, 
   new { CssClass = "input-config", onchange = "this.form.submit();" })%>

When changing the selection, I call post action. After the message, the same page is displayed on which this drop-down list is present. I want to know about the HTML attribute for the drop-down list, which will allow me to save the selection change of the list. But at the moment, the list shows its first element after publication. for example, Dropdoen contains elements like 1,2,3, etc. The default value is 1. If I select 2, the message is called and the same page is displayed again, but my choice is 2, and 1 is selected again. How to save a choice?

Thanks Kapil

+3
source share
4

, ( Selected SelectListItem).

0

, ViewData [ "Available" ], select. , html, . , :

ViewData["Available"] = new SelectList( items, "dataValueField", "dataTextField", "selectedValue" );
0

:

<% = Html.DropDownList( "Ibus", ViewData [ "Ibus" ] SelectList, { @class= "dASDropDown" })% >

0

You need to take a property model ddlor get it as a parameter in action, for example:

public ActionResult Action(Model model, string ddl) 

Then, to create ViewData [" Available "], you must pass it as the selected value

public ActionResult Action(Model model, string ddl) 
{
   ViewData["Available"] = List<SelectListItem>
        {
            new SelectListItem { Text = "1", Value = "1", Selected = (ddl == "1") },
            new SelectListItem { Text = "2", Value = "2", Selected = (ddl == "2") },
            new SelectListItem { Text = "3", Value = "3", Selected = (ddl == "3") }
        };

   return View(model);
}

OR

public ActionResult Action(Model model, string ddl) 
{
   var list = List<SelectListItem>
        {
            new SelectListItem { Text = "1", Value = "1" },
            new SelectListItem { Text = "2", Value = "2" },
            new SelectListItem { Text = "3", Value = "3" }
        };

   ViewData["ddl"] = new SelectList(list, "value", "text", ddl);
   return View(model);
}

EDIT : see also this

0
source

Source: https://habr.com/ru/post/1731863/


All Articles