我可以在 ASP.NET MVC 中使用 linq 执行 foreach 列吗?
我正在使用 telerik ASP.NET MVC 网格模板,当前有几个复选框(表中的每一列都有一个)。这样用户就可以选择他们想要在网格中显示的列。
我可以使用 foreach 循环在视图上生成每个复选框,而不是手动写出视图上的每个复选框吗?
我正在寻找替换这样的东西:
<%= Html.CheckBox("SomeColumnID", false, "Something")%><label for="SomeColumnID">Some Label</label>
// . . . over and over again for each column
用这样的东西:
<%foreach (ColumnInGivenDB)
{%>
<%= Html.CheckBox(SomeColumnIDVariable, false, SomeOtherVariable)%><label for=SomeColumnIDVariable>Some Label</label>
<%}%>
有什么想法吗?
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, "Something")%><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?
看来您已经快到了,但缺少与
Model
的绑定,或者缺少与模型属性部分的绑定。你的意思是像下面这样吗?
作为示例,我绑定到具有 UserName 属性的简单对象
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