开发者

Can I do a foreach Column with linq in ASP.NET MVC?

I am using the telerik ASP.NET MVC Grid template and currently have several check boxes (one for each column in a table). This way the user can select which columns they want to be displayed in the grid.

Instead of manually writing out each checkbox on the view, can I use a foreach loop to have each check box generated on the view for me?

I'm looking to replace something like this:

<%= Html.CheckBox("SomeColumnID", false, "Somethi开发者_如何学编程ng")%><label for="SomeColumnID">Some Label</label>
// . . . over and over again for each column

With something like this:

<%foreach (ColumnInGivenDB)
{%>
   <%= Html.CheckBox(SomeColumnIDVariable, false, SomeOtherVariable)%><label for=SomeColumnIDVariable>Some Label</label>
<%}%>

Any ideas?


It seems you're almost there, but missing binding to the Model or one if its properties part.

You mean something like the following?

As an example I am binding to a simple object with UserName property

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%=Html.CheckBox(item.UserId, (IsChecked logic here)) %>
            <label for="<%=item.UserId%>"><%=item.UserName %></label>
        </td>

    </tr>

<% } %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜