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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8668889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:39:34  来源:igfitidea点击:

Can I change the html name generated by Html.DropDownListFor?

asp.net-mvcasp.net-mvc-3

提问by Samantha J T Star

IDI am using the following code to create a drop down list:

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> 

The code creates the following:

该代码创建以下内容:

<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> 

Is there any way that I could change it so that it creates a IDof "Status_2", "Status_3" etc.

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

回答by hmz

Like @Anar said in the comments;

就像@Anar 在评论中所说的那样;

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

实际上,您可以使用与 id 相同的方式更改 name 属性,但不要使用“name”,而是使用“Name”。令人惊讶的是它有效。

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

回答by nuander

Sometimes the HTML helpers don't HELP. DropDownListFor coding can get complicated real fast and in the end it's just rendering HTML so sometimes it's better to go old school

有时 HTML 助手没有帮助。DropDownListFor 编码会很快变得复杂,最后它只是渲染 HTML,所以有时最好去老派

<select name="myname" class="dropdownlist" id="myid">
    @foreach (SelectListItem item in Model.SomeSelectList) {
        if (item.Value ==  Model.theValue) {
            <option value="@(item.Value)" selected="selected">@item.Text</option>
        } else {
            <option value="@(item.Value)">@item.Text</option>
        }
    }
</select>

回答by mtholen

For those wondering why this doesn't work, the whole thing is case sensitive and need an @ in front of it...

对于那些想知道为什么这不起作用的人来说,整个事情都是区分大小写的,并且需要在它前面加上一个@...

so, this works:

所以,这有效:

new { @id = "myID", @Name = "myName", @Value = "myValue", @class = "form-control", @onchange = "javascript:DoSomething(this.value);" }

And this doesn't (mind the lowercase 'n' in @name)

而这不会(请注意中的小写 'n' @name

new { @id = "myID", @name = "myName", @Value = "myValue", @class = "form-control", @onchange = "javascript:DoSomething(this.value);" }

回答by Arbiter

You can set/change the ID just like any HTML attribute (but not name I've found).

您可以像任何 HTML 属性一样设置/更改 ID(但不是我找到的名称)。

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

回答by Jitendra Bansiwal

Just use "Name" instead of "name" and it works.

只需使用“名称”而不是“名称”即可。

@Html.DropDownList("ClassID", null, "Select Class", htmlAttributes: new { id = _id, Name =_id, @class = "form-control" })

回答by TZHX

ID can be changed as described by @Arbiter. However, if you want to change the name, then I think you need to use @Html.DropDownListrather than @Html.DropDownListFor. This loses you any benefits of having the strong typing for the property element, but as just changing IDs means you can't access the values in a meaningful way if looking at the Request.Formresponse, this may be an acceptable workaround for you.

可以按照@Arbiter 的描述更改 ID。但是,如果您想更改名称,那么我认为您需要使用@Html.DropDownList而不是@Html.DropDownListFor. 这将失去为属性元素提供强类型的任何好处,但由于仅更改 ID 意味着如果查看Request.Form响应,您将无法以有意义的方式访问值,因此这对您来说可能是一种可以接受的解决方法。

回答by TZHX

You need to change From DropDownListFor To DropDownList. Than you can change name easily.

您需要将 From DropDownListFor 更改为 DropDownList。比您可以轻松更改名称。

@Html.DropDownList("YourName", SelectList, new { @id = "YourId", @class = "YourClass" })

Hope It will work.

希望它会起作用。

回答by Yugam

Thanks .. it work...adding .Name change the name of the html password attribute

谢谢 .. 它工作...添加 .Name 更改 html 密码属性的名称

      @Html.PasswordFor(Function(model) model.password, New With {.Autocomplete = "off", .Name = "oldpassword", .id = "oldpassword", .value = Model.password, .readonly = True})

回答by RitchieD

My solution to this issue is to replace the id and name after DropDownListFor() with a html helper extension method.

我对这个问题的解决方案是将 DropDownListFor() 之后的 id 和 name 替换为 html helper 扩展方法。

        public static MvcHtmlString ReplaceIdAndName(this MvcHtmlString htmlSource, string name)
    {
        MvcHtmlString result;
        if (name == null)
            result = htmlSource;
        else
        {
            string id = name.Replace("[", "_").Replace("]", "_").Replace(".", "_");  // HTML ids cannot have []. so MVC convention replaces with _
            XDocument html = XDocument.Parse(htmlSource.ToHtmlString());
            html.Elements().Select(e => e.Attribute("id")).FirstOrDefault().SetValue(id);
            html.Elements().Select(e => e.Attribute("name")).FirstOrDefault().SetValue(name);
            result = MvcHtmlString.Create(html.ToString());
        }
        return result;
    }


DropDownListFor(...).ReplaceIdAndName("myclass[1].myprop");