我有一个键/值对的列表。基本上它是一个列表,其中ViewModel是表单的自定义类
public class ViewModel
{
public String Key { get; set; }
public String Value { get; set; }
}在视图中,我需要为键和值呈现标签和文本框,尝试使用Html.DisplayFor(),但是它与respectively.Im一起使用,并且只显示模型的属性,而不显示列表。
我想实现一些格式上的东西
<% foreach (var item in Model) { %>
<tr>
<td>
<%:Html.Display("item")%>
</td>
<td>
<%:Html.Display("item.Value")%>
</td>
</tr>
<% } %>发布于 2011-01-13 16:41:18
您可以尝试在主视图中使用编辑器模板,该模板将为模型的每个项目呈现(假设您的模型是一个集合)。编辑器模板比显示模板更适合您的方案,因为您呈现的是允许编辑的文本框。因此,使用EditorFor而不是DisplayFor在语义上更正确
<table>
<%= Html.EditorForModel() %>
</table>然后为视图模型定义编辑器模板(~/Views/Home/EditorTemplates/ViewModel.ascx):
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<YourAppName.Models.ViewModel>" %>
<tr>
<td>
<%: Model.Key %>
</td>
<td>
<%= Html.TextBoxFor(x => x.Value) %>
</td>
</tr>https://stackoverflow.com/questions/4677631
复制相似问题