asp.net MVC 的 Html.RadioButton 生成 id 和 name 相同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9291975/
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-08-28 22:34:08  来源:igfitidea点击:

Html.RadioButton of asp.net MVC generates id and name same

htmlasp.net-mvc

提问by Ali

<%=Html.RadioButton("BookType", "1")  %> C#
<%=Html.RadioButton("BookType", "2")  %> VB

Above code generates Below code

上面的代码生成下面的代码

<input id="BookType" name="BookType" type="radio" value="1" >C#
<input id="BookType" name="BookType" type="radio" value="2" >VB

I need same name but different ids

我需要相同的名称但不同的 ID

I want output like

我想要输出像

*

*

<input id="rdoCSharp" name="BookType" type="radio" value="1" >C#
<input id="rdoVb" name="BookType" type="radio" value="2" >VB

*

*

回答by Rich O'Kelly

You can override the defaults by passing the attributes you wish to apply using an overload of the RadioButton method:

您可以通过使用RadioButton 方法重载传递您希望应用的属性来覆盖默认值:

<%=Html.RadioButton("BookType", "1", new { id = "yourId" })  %> C#

回答by Darin Dimitrov

<%= Html.RadioButton("BookType", "1", new { id = "rdoCSharp" }) %> C#
<%= Html.RadioButton("BookType", "2", new { id = "rdoVb" }) %> VB

回答by Hadas

Use:

用:

<%=Html.RadioButton("BookType", "1",new{@id="BookType1"})  %> C#
<%=Html.RadioButton("BookType", "2",new{@id="BookType2"})  %> VB