Asp.Net MVC DropDownList 数据绑定 [英] Asp.Net MVC DropDownList Data Binding
问题描述
<form id="Form1" runat="server">
<asp:DropDownList ID="dvmDrmList" runat="server">
<asp:ListItem>Theory</asp:ListItem>
<asp:ListItem>Appliance</asp:ListItem>
<asp:ListItem>Lab</asp:ListItem>
</asp:DropDownList>
</form>
我想在控制器中绑定这个 DropDownList.我的意思是如何在控制器类的操作方法中获取 dropDownList 的值.谢谢.
I want to bind this DropDownList in controller. I mean how can I get the value of the dropDownList in the action method in controller class. Thanks.
推荐答案
我看到您正在使用带有 runat="server"
和 asp:XXX
网络控件的表单.这些是永远不应在 ASP.NET MVC 中使用的概念.这些服务器控件不再依赖于 ViewState 和 PostBack.
I see that you are using forms with runat="server"
and asp:XXX
web controls. Those are notions should never be used in ASP.NET MVC. There is no more ViewState and PostBacks that those server controls rely on.
因此,在 ASP.NET MVC 中,您首先要定义一个表示数据的视图模型:
So in ASP.NET MVC you would start by defining a view model representing the data:
public class ItemsViewModel
{
public string SelectedItemId { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
}
然后您将定义一个具有两个动作的控制器(一个用于呈现视图,另一个用于处理表单提交):
then you would define a controller with two actions (one that renders the view and another that handles the form submission):
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new ItemsViewModel
{
Items = new[]
{
new SelectListItem { Value = "Theory", Text = "Theory" },
new SelectListItem { Value = "Appliance", Text = "Appliance" },
new SelectListItem { Value = "Lab", Text = "Lab" }
}
};
return View(model);
}
[HttpPost]
public ActionResult Index(ItemsViewModel model)
{
// this action will be invoked when the form is submitted and
// model.SelectedItemId will contain the selected value
...
}
}
最后你将编写相应的强类型Index
视图:
and finally you would write the corresponding strongly typed Index
view:
<%@ Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<AppName.Models.ItemsViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) { %>
<%= Html.DropDownListFor(x => x.SelectedItemId, new SelectList(Model.Items, "Value", "Text")) %>
<input type="submit" value="OK" />
<% } %>
</asp:Content>
据说您也可以在视图中对此选择进行硬编码(尽管我不推荐这样做):
This being said you could also hardcode this select inside your view (although this is something I wouldn't recommend):
<% using (Html.BeginForm()) { %>
<select name="selectedItem">
<option value="Theory">Theory</option>
<option value="Appliance">Appliance</option>
<option value="Lab">Lab</option>
</select>
<input type="submit" value="OK" />
<% } %>
并具有以下控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string selectedItem)
{
// this action will be invoked when the form is submitted and
// selectedItem will contain the selected value
...
}
}
这篇关于Asp.Net MVC DropDownList 数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!