asp.net-mvc-2 – Asp.Net MVC DropDownList数据绑定
发布时间:2020-12-16 00:18:23 所属栏目:asp.Net 来源:网络整理
导读:form id="Form1" runat="server" asp:DropDownList ID="dvmDrmList" runat="server" asp:ListItemTheory/asp:ListItem asp:ListItemAppliance/asp:ListItem asp:ListItemLab/asp:ListItem /asp:DropDownList /form 我想在控制器中绑定此DropDownList.我的意
<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.我的意思是如何在控制器类的action方法中获取dropDownList的值.谢谢. 解决方法
我看到你使用的形式有runat =“server”和asp:XXX web控件.这些概念永远不应该在ASP.NET MVC中使用.这些服务器控件不再依赖于ViewState和PostBack.
因此,在ASP.NET MVC中,您将首先定义表示数据的视图模型: public class ItemsViewModel { public string SelectedItemId { get; set; } public IEnumerable<SelectListItem> Items { get; set; } } 然后你将定义一个带有两个动作的控制器(一个呈现视图,另一个呈现表单提交): 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> 这就是说你也可以在你的视图中硬编码这个选择(虽然这是我不推荐的): <% 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控制器和视图应该有1到1的关系吗?
- ASP.NET MVC项目中的Web表单?
- asp.net-mvc – 在ASP.NET MVC 3中使用Ajax响应发送的自定义
- 更改ASP.NET XHTML渲染模式
- 打印友好的ASP.NET MVC 3视图
- asp.net-mvc – Umbraco Surface Controller或RenderMvcCon
- com – 在HTTPD中调试ASP页面
- .NET Core的日志[3]:将日志写入Debug窗口
- asp.net-mvc – 在实体框架中我只想包括第一个子对象而不是
- asp.net-mvc – 如何在SelectList文本描述中组合两个字段?
推荐文章
站长推荐
- asp.net-mvc-5 – Sharepoint 2013 MVC 5提供商托
- iis-7 – 从IIS 7上的经典ASP创建COM组件时出现“
- ASP.NET 4中仍然需要App_Browsers文件夹吗?
- asp.net-mvc-4 – MVC 4.0 WebApi应用程序和混合
- asp.net – 可以承受1000个并发用户的Web服务,响
- asp.net mvc – 如何实现面包屑助手在asp.net mv
- 什么perfmon计数器可用于识别ASP.NET瓶颈?
- asp.net-mvc – 有没有人能够或者至少试图将液体
- asp.net – OData和区分大小写
- asp.net – 根据文化显示正确的日期格式
热点阅读