asp.net-mvc – 可以更改Html.DropDownListFor生成的html名称吗?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 可以更改Html.DropDownListFor生成的html名称吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
IDI正在使用以下代码创建一个下拉列表:
@for (var index = 0; index < Model.AdminSummaries.Count(); index++) 
            { 
            <div class="rep_tr0"> 
                <div class="rep_td0"> 
                    @Html.DropDownListFor(x => Model.AdminSummaries[index].Status,AdminStatusReference.GetAdminStatusOptions()) 
                </div>

代码创建以下内容

<select id="AdminSummaries_2__Status" name="AdminSummaries[2].Status">
   <option value="1">Released</option>
   <option value="2">Review</option>
   <option value="3">New</option>
</select> 
<select id="AdminSummaries_3__Status" name="AdminSummaries[3].Status">
   <option value="1">Released</option>
   <option value="2">Review</option>
   <option value="3">New</option>
</select>

有什么办法可以改变它,以便创建一个“Status_2”,“Status_3”等的ID。

解决方法

像@Anar在评论中说的;

Actually,you can change the name attribute the same way as id,but instead of “name”,use “Name”. Surprisingly it works.

@Html.DropDownListFor(x => Model.AdminSummaries[index].Status,AdminStatusReference.GetAdminStatusOptions(),new { id = string.Format("Status_{0}",index ),Name = "GiveName" });

猜你在找的asp.Net相关文章