【发布时间】:2011-01-25 10:58:43
【问题描述】:
我相信这个问题适用于任何“For”Html 助手,但我的具体问题是使用 CheckBoxFor...
我有一个 IEnumerable 类型的模型,其中权限是一个简单的 POCO。这个模型实际上是一个更大模型的属性,我为其创建了一个 EditorTemplate。这是我的模型的大图:
public class bigmodel
{
public string Title {get; set;}
public string Description {get; set;}
[UIHint("ListRights")]
public IEnumerable<rights> Rights {get;set;}
}
public class rights
{
public bool HasAccess {get; set;}
public string Description {get;set;}
}
我创建了一个名为“ListRights”的编辑器模板,我的主视图使用它。例如: m.Rights) %>。
在 ListRights.ascx 中,我想要这样的代码:
<table>
<% foreach(rights access in Model)
{ %>
<tr>
<td>
<%=Html.CheckBoxFor( access ) %>
</td>
<td>
<%=access.Description %>
</td>
</tr>
<% } %>
</table>
我知道 CheckBoxFor 行不起作用,但我想做一些产生相同结果的事情,就好像 access 是模型上的一个属性一样。
在上面的示例中,我希望所有内容都在发布时自动绑定。
我尝试使用类似的代码伪造 CheckBox,但它不会自动绑定:
<table>
<% for(int i=0; i < Model.Count(); i++)
{ %>
<tr>
<td>
<%=Html.CheckBox(string.Format("[{0}].HasAccess",i), Model.ElementAt(i).HasAccess)%>
</td>
<td>
<%=access.Description %>
</td>
</tr>
<% } %>
</table>
有什么建议吗?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-2 html-helper