Ali
Ali

Reputation: 3615

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

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

I want output like

*

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

*

Upvotes: 29

Views: 55273

Answers (3)

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

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

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

Upvotes: 38

Darin Dimitrov
Darin Dimitrov

Reputation: 1038750

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

Upvotes: 9

Hadas
Hadas

Reputation: 10374

Use:

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

Upvotes: 5

Related Questions