在ASP.NET MVC3中如何使用单个选择绑定并向列表框提供操作

时间:2012-11-01 17:05:44

标签: c# asp.net-mvc-3

在asp.net mvc3 razor中查看如何使用单个选择绑定列表框中的字符串列表。 我正在尝试使用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>

我遇到的问题是: 1.如何在列表框中显示学生的姓名。 2.如何通过选择提供动作。

1 个答案:

答案 0 :(得分: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>