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.
推荐答案
我看到你使用表单的 =服务器
和 ASP:XXX
Web控件。这些都是概念不应该在ASP.NET MVC中使用。有没有更多的ViewState和回传,那些服务器控件的依赖。
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,你会通过定义视图模型重新presenting数据开始:
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
...
}
}
最后你会写相应的强类型首页
查看:
<%@ 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>
这就是说,你还可以硬code此选择您的视图中(虽然这是我不会推荐):
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屋!