Mik*_*ien 11 asp.net-mvc html-helper asp.net-mvc-2
我相信这个问题适用于任何"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;}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个名为"ListRights"的editortemplate,我的主视图使用它.例如:<%= Html.EditorFor(m => m.Rights)%>.
在ListRights.ascx中,我想要这样的代码:
<table>
<% foreach(rights access in Model)
{ %>
<tr>
<td>
<%=Html.CheckBoxFor( access ) %>
</td>
<td>
<%=access.Description %>
</td>
</tr>
<% } %>
</table>
Run Code Online (Sandbox Code Playgroud)
我知道CheckBoxFor行不起作用,但我想做一些生成相同结果的东西,就像访问是模型上的属性一样.
在上面的例子中,我希望所有内容都能在帖子上自动查找.
我尝试使用类似于此的代码伪造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>
Run Code Online (Sandbox Code Playgroud)
有什么建议?
mar*_*are 12
我猜你有问题,因为这不起作用
<%=Html.CheckBoxFor(access) %>
Run Code Online (Sandbox Code Playgroud)
这也不起作用
<%=Html.CheckBoxFor(access=>access.HasAccess) %>
Run Code Online (Sandbox Code Playgroud)
但这应该有效
<%=Html.CheckBoxFor(x=>access.HasAccess) %>
Run Code Online (Sandbox Code Playgroud)
我通过Steve Sanderson的博客文章在http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/找到了答案.
使用"Html.BeginCollectionItem"在我的情况下工作.
我为权限创建了一个EditorTemplate(在我的例子中).然后将Steve的BeginCollectionItem添加到该模板中.我按史蒂夫博客中的建议使用Html.RenderPartial调用了模板.
我想使用Html.EditorFor(m => m.item),但这不起作用,因为item在ForEach中,而不在模型中.在这种情况下可以使用EditorFor吗?
归档时间: |
|
查看次数: |
20112 次 |
最近记录: |