0

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.

4

1 回答 1

0

尝试这个:

你的控制器

public ActionResult Student()
{
    List<StudentInfo> liststudent=new LIst<StudentInfo>{
                        new StudentInfo{id="v11",names="AB"},
                        new StudentInfo{id="v12",names="SA"}}
    return View(liststudent);
}

你的看法

@model List<MvcApplication1.Models.StudentInfo>

<div class="alignright">
    @using (Html.BeginForm())
    {
    @Html.ListBox("MyList",new SelectList(model, "id", "names"));
    }
    </div>
于 2012-11-01T19:38:57.763 回答