簡體   English   中英

如何在ASP.NET MVC視圖的foreach循環中設置tabindex?

[英]How can I set a tabindex in a foreach loop in an ASP.NET MVC view?

如何在ASP.NET MVC的foreach循環中設置一組復選框的tabindex值?

我當前的解決方案使用Response.Write輸出復選框,但我希望找到更優雅的方法。

有沒有人有一個好的解決方案?

此代碼不起作用:

<label for="country">
    Country:
</label>
<%= Html.DropDownList("country", Model.Countries.OrderBy(x => x.Text), "Choose country...", new { tabindex = 8 })%><br />

<fieldset id="sales-processing-methodologies">
    <legend>Sales Processing Methodology:</legend>
    <% int i = 9;
    foreach (var method in Model.ProcessingMethodologies)
    {
    %>
        <input type="checkbox" id="<%=method.Key%>" name="processing-methodologies" class="checkbox" value="<%=method.Key%>" tabindex="<%i.ToString();%>"> </input>
        <label for="<%=method.Key%>">
            <%=method.Value%>
        </label>
    <% i++;
    } %>
 </fieldset>

這樣做:

<label for="country">
    Country:
</label>
<%= Html.DropDownList("country", Model.Countries.OrderBy(x => x.Text), "Choose country...", new { tabindex = 8 })%><br />

<fieldset id="sales-processing-methodologies">
    <legend>Sales Processing Methodology:</legend>
    <% int i = 9;
    foreach (var method in Model.ProcessingMethodologies)
    {
        Response.Write(@"<input type=""checkbox"" id=""" + method.Key + @""" name=""processing-methodologies"" class=""checkbox"" value=""" + method.Key + @""" tabindex=""" + i.ToString() + @"""> </input><label for=""" + method.Key + @""">" + method.Value + @"</label>");
        i++;
    } %>
</fieldset>

編輯:無法正常工作的代碼產生了tabindex =“”

我只是做一個干編譯(在我的大腦中),您的代碼沒什么大不了的。 請同時在此處發布編譯器錯誤。

唯一錯誤的一件事是:

<input type="checkbox" id="<%=method.Key%>" name="processing-methodologies" class="checkbox" value="<%=method.Key%>" tabindex="<%i.ToString();%>"> </input>

tabindex =“ <%* = * i.ToString();%>”缺少等號。

只需使用for循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM