如何在MVC中添加Id到单选按钮?

时间:2015-06-24 09:33:19

标签: asp.net-mvc asp.net-mvc-4 razor

我想在我的单选按钮中添加唯一ID。我的代码是用foreach循环编写的。因为它为所有相同的按钮生成相同的名称和Id,但我想要相同的名称,但Id应该是不同的。我应该在哪里添加Id?请帮忙。我的代码如下:

 <fieldset class="TimeSection">
    <legend class="accessibleText"> <%= Resources.ChooseTime%> </legend>  
        <% var count = 0;%>
        <% foreach (var slot in Model.Slots)
           { %>
        <% if (count < 5)
           { %>
        <span>          
            <%=Html.RadioButton(model => model.Arrangement,slot.Number, slot.Available == false ? new { disabled = "disabled" } : null)%>

            <span <% if (!slot.Available){ %> class="disabled" <% } %>>
                <%= slot.SlotTime%></span>
            <% if (slot.Number == Model.ArrivalTimeslot)
               { %>
            <input type="hidden" name="ExistingTimeslot" value="<%=slot.Number%>" />
            <% } %>
        </span>
        <br />
        <br />
        <% } %>
        <% count = count + 1; %>
        <% } %>  
</fieldset>

1 个答案:

答案 0 :(得分:0)

<%=Html.RadioButton(model => model.Arrangement,slot.Number, slot.Available == false ? new { disabled = "disabled" } : null)%>

应该是 -

<%=Html.RadioButton(model => model.Arrangement,slot.Number, slot.Available == false ? (object)new { disabled = "disabled", id = String.Format("SlotID{0}", count.ToString()) } : new { id = String.Format("SlotID{0}", count.ToString()) })%>

关键点id = String.Format("SlotID{0}", count.ToString())