0

I am using MVC3 with Razor.

I would like to generate id's for my radio button dynaically. I am calling my code in loop and the id's / name's should be generated dynaically. Name to my radiobutton in my View...like below code....

@Html.RadioButtonFor(m => m.ClaimEntity.Claim, "True", Model.m.ClaimEntity.Claim.Equals(true) ? new { name = "Claim_" + ViewData["count"].ToString(), id = "Claim", title = "Claim_" + ViewData["count"].ToString() } : null)
                @Html.Label("Yes")
                @Html.RadioButtonFor(m => m.ClaimEntity.Claim, "False", Model.m.ClaimEntity.Claim.Equals(false) ? new { name = "Claim_" + ViewData["count"].ToString(), id = "Claim", title = "Claim_" + ViewData["count"].ToString() } : null)
                @Html.Label("No")

Problem is, the above code snippet does not show the dynamic id created for radiobutton whereas it shows the "title" created dynaimically, so I need something on similar line for "title" for "id" and "name" attribute.

Plz suggest how can i get this working. Regards,

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
Aryan
  • 374
  • 1
  • 2
  • 14
  • Here is some good guidance for using enums with radio buttons: http://stackoverflow.com/q/5621013/109941 – Jim G. Sep 12 '12 at 18:58

1 Answers1

0

It looks like all the radiobuttons you're generating have the same id! This can cause unexpected behavior in your webpage: as a rule, each element must have a unique id.

If we make the id dependent on ViewData["count"] as well, we can make sure they're unique.

@Html.RadioButtonFor(m => m.ClaimEntity.Claim, "True", Model.m.ClaimEntity.Claim.Equals(true) ? new { name = "Claim_" + ViewData["count"].ToString(), id = "Claim_True" + ViewData["count"].ToString(), title = "Claim_" + ViewData["count"].ToString() } : null)
                @Html.Label("Yes")
                @Html.RadioButtonFor(m => m.ClaimEntity.Claim, "False", Model.m.ClaimEntity.Claim.Equals(false) ? new { name = "Claim_" + ViewData["count"].ToString(), id = "Claim_True" + ViewData["count"].ToString(), title = "Claim_" + ViewData["count"].ToString() } : null)
                @Html.Label("No")
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
Khelvaster
  • 852
  • 1
  • 6
  • 18