How to give names for dynamically generated radiobuttonfor

1.6k views Asked by At

I am generating radiobuttonfor dynamically. My code runs as,

@{int questionCount = 0;
 }
@{foreach (SectionDetail sectionDetail in section.SectionDetails)
 {                              
    <table>
        <tr>
            <td>
                @Html.Label(sectionDetail.Title)
            </td>
            @{count = 0;
            }
            @foreach (SectionScore sectionScore in Model.IndividualSections)
            {

                <td class="ColSupplier">
                    @Html.RadioButtonFor(model => model.IndividualSections[count].SectionScores[questionCount].score.pass, true)Pass
                    @Html.RadioButtonFor(model => model.IndividualSections[count].SectionScores[questionCount].score.pass, false)Fail
                </td>
               count++;
            }
        </tr>
    </table>

    questionCount++;
  }

Here some radio buttons are having the same name. So I am unable to get the exact value. How to give different names for these radio buttons. Please help me in sorting this out.

2

There are 2 answers

1
Muhammad Mudassir On BEST ANSWER

Please try this

  @(Html.RadioButtonFor(model => model.IndividualSections[count].SectionScores[questionCount].score.pass, true, new {@id=sectionDetail.Title + count.Tostring() }) ))
1
Anwar On

You can use HtmlAttributes to define unique IDs.

@Html.RadioButtonFor(model => model.IndividualSections[count].SectionScores[questionCount].score.pass, true, new { id= count.ToString()});

Hope this helps you.