ASP. net mvc sets the method of the selected item for DropDownListFor, mvcdropdownlistfor
In MVC, when a strong type editing page is involved, if a select element exists, you need to Select a select item based on a certain attribute value of the current Model. This article only organizes ideas and does not involve complete code.
□Ideas
The type of the forward view is List <SelectListItem>. Set the Selected Attribute of the Selected SelectListItem to true, and put the object instance of this type in ViewBag, ViewData or Model to the foreground view.
Traverse List <SelectListItem> type object instances
□Controller
Public ActionResult SomeAction (int id) {// obtain Domain Model var domainModel = ModelService from the database. loadEntities (m => m. ID = id ). firstOrDefault <Model> (); // obtain the List <SelectListItem> type object instance List <SelectListItem> items = SomeMethod () through a method; // traverses the set, if an attribute of the current Domain model is the same as the Value attribute of SelectListItem, set the Selected Attribute of SelectListItem to true foreach (SelectListItem item in items) {if (item. value = Convert. toString (domainModel. A property) {item. selected = true ;}/// put the List <SelectListItem> set object instance in ViewData ["somekey"] = items; // It may involve converting Domain Model to View Model return PartialView (domainModel );}
□Foreground View display
@ Model DomainModel
@ Html. DropDownListFor (m => m. SomeProperty, (List <SelectListItem>) ViewData ["somekey"], "= select = ")
Traverse the Model set
Set a bool field for View Model to check whether the field is selected.
Some attributes of the Model are used as the Text and Value values of SelectListItem. Determine whether to set SelectListItem's Selected to true based on the Boolean attribute in View Model.
□View Model
public class Department{ public int Id {get;set;} public string Name {get;set;} public bool IsSelected {get;set;}}
□Controller
Public ActionResult Index () {SampleDbContext db = new SampleDbContext (); List <SelectListItem> selectListItems = new List <SelectListItem> (); // traverse the Department set foreach (Department department in db. parameters) {SelectListItem = new SelectListItem {Text = department. name, Value = department. id. toString (), Selected = department. isSelected. hasValue? Department. IsSelected. Value: false} selectListItems. Add (selectListItem);} ViewBag. Parameters = selectListItems; return View ();}
The following is a supplement from other netizens:
Background code:
Public ActionResult Index (FormCollection collection) {IList <Project> li = Utility. sqlHelper. getProjectList (); SelectList selec = new SelectList (li, "ID", "Name"); if (collection ["drop"]! = Null) {string projectID = collection ["drop"]; selec = new SelectList (li, "ID", "Name", projectID ); // set the selected ViewData ["ruturned"] = collection ["drop"];} ViewData ["drop"] = selec; return View ();}
Front-end code:
@ Using (Html. BeginForm ()){
@ Html. DropDownList ("drop", ViewData ["d"] as SelectList)
<Input type = "submit" value = "view the corresponding group list"/>
}
<P> Current Project ID: @ ViewData ["ruturned"] </p>
How can I use aspnet mvc2 DropDownListFor to select a specified value for DropDownListFor?
Depends on the value of FieldType,
Or set the selected value in the SelectList of TypesModelList.
Net mvc does not know if it is a bug. When using HtmlDropDownListFor, there is no way to set the selected project.
Of course it's not a BUG.
@ Model Models. Blog
@ Html. DropDownListFor (m => m. Field name, list sequence)
If the Model is null, of course there is no selected item (or the first item will be selected ).