0

我有一个复选框列表,例如:

<% foreach (var tobj in (ViewData["terr"] as List<Location>))
{ %>
   <input type="checkbox" name="terr" id="<%: tobj.Location_Id %>" value="<%: tobj.Location_Id %>" disabled="disabled"/> 
   <span id="<%: tobj.Location_Name %>"> <%: tobj.Location_Name %> </span>
   <br />
<% } %>

但我想在一行中显示 3 或 4 个复选框。为此,我需要将 foreach 转换为 for 循环。我无法做到这一点。希望任何人都可以在这方面帮助我。

4

1 回答 1

0

我不确定为什么你不能在 foreach 循环中完成你需要的东西。如果您用更多信息更新了您的问题,我们可能会帮助您调试初始 foreach 循环。此外,如果您使用 MVC 3,我推荐 Razor 视图引擎,语法会好很多。

for 循环等效项:

<% { var locations = (List<Location>)ViewData["terr"] } %>

<% for (int i = 0; i < locations.Count(); i++)
{ %>
    <input type="checkbox" name="terr" id="<%: locations[i].Location_Id %>" value="<%: locations[i].Location_Id %>" disabled="disabled"/> 
    <span id="<%: locations[i].Location_Name %>"> <%: locations[i].Location_Name %> </span>
    <br />
<% } %>
于 2013-03-09T07:32:38.787 回答