In asp.net mvc3 razor view how to bind the list of strings in the listbox with the single selection. I am trying to bind list of Names (List) with the ListBox.
**Model**
Class StudentInfo
{
public string id{get,set};
public string names{get,set};
}
**In Conrtoller**
[HTTPGet]
public ActionResult Student()
{
List<StudentInfo> liststudent=new LIst<StudentInfo>{
new StudentInfo{id="v11",names="AB"},
new StudentInfo{id="v12",names="SA"},
return View(liststudent)
}
}
[HTTPPost]
public ActionResult Student(string name)
{
return View()
}
}
**In View**
<div class="alignright">
@using (Html.BeginForm())
{
@Html.ListBox("MyList",new SelectList(???));
}
</div>
The problem I am encountering are: 1. How to display the names of the student in the listbox. 2. How to provide an action with the selection.
Try this:
your controller
public ActionResult Student()
{
List<StudentInfo> liststudent=new LIst<StudentInfo>{
new StudentInfo{id="v11",names="AB"},
new StudentInfo{id="v12",names="SA"}}
return View(liststudent);
}
your view
@model List<MvcApplication1.Models.StudentInfo>
<div class="alignright">
@using (Html.BeginForm())
{
@Html.ListBox("MyList",new SelectList(model, "id", "names"));
}
</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.