asp.net-mvc - MVC : How do i declare the name value using DropDownListFor

标签 asp.net-mvc

我在 mvc 中有以下下拉列表

 <%= Html.DropDownListFor(c => c.Address.Id, new SelectList(Model.Addresses, "id", "Name", Model.Address.Id), "-- New Address --", new { @name = "[" + orderItemAddressCount + "].AddressId" })%>

我正在尝试覆盖下拉列表中的名称值。

这是我得到的标记
 <select id="Address_Id" name="Address.Id"><option value="">-- New Address --</option>

这是我想要的标记
 <select id="Address_Id" name="[0].AddressId"><option value="">-- New Address --</option>

如何使用 DropDownListFor 声明名称值?

最佳答案

您不能覆盖 DropDownListFor 生成的名称。方法。它是强类型的,名称基于您作为参数传递的 lambda。如果要覆盖生成的名称,请选择所需的方式,您必须编写自己的扩展方法或使用非强类型 DropDownList方法:

<%= Html.DropDownList(
    "[0].AddressId", 
    new SelectList(Model.Addresses, "id", "Name", Model.Address.Id), 
    "-- New Address --") 
%>

但是你为什么要这样做呢?提交表单时不是绑定(bind)到同一个 ViewModel 吗?

关于asp.net-mvc - MVC : How do i declare the name value using DropDownListFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2844927/

相关文章:

asp.net-mvc - Azure 上托管网站的计费方式 (asp.net mvc)

javascript - 如何仅在加载所有脚本时调用函数?

c# - HttpUtility HtmlDecode。为什么我需要这个?

asp.net-mvc - HTML5 离线 list /功能如何与 ASP.NET MVC 4 配合使用?

asp.net-mvc - MVC : How to use (render) sections from parent (master) View (Layout View)?

c# - 在 MVC 5 中使用 EF 选择特定列

c# - Newtonsoft 忽略属性?

html - ASP.NET MVC View 对齐文本框

asp.net-mvc - 如何生成一个自定义按钮,在 Kendo UI Grid MVC 中从其 uid 提交单元格数据?

c# - 在 C# 中使用索引数组反序列化 JSON