Asp.Net MVC中用于项目列表的Html.DisplayFor()
我有一个键/值对列表。基本上,它是一个列表,其中ViewModel是表单的自定义类Asp.Net MVC中用于项目列表的Html.DisplayFor(),asp.net,asp.net-mvc-2,Asp.net,Asp.net Mvc 2,我有一个键/值对列表。基本上,它是一个列表,其中ViewModel是表单的自定义类 public class ViewModel { public String Key { get; set; } public String Value { get; set; } } 在视图中,我需要分别为Key和Value呈现Label和Textbox。我尝试使用Html.DisplayFor(),但是它与model一起使用,只显示模型的属性,而不显示列表 我想达到某种形式 <% f
public class ViewModel
{
public String Key { get; set; }
public String Value { get; set; }
}
在视图中,我需要分别为Key和Value呈现Label和Textbox。我尝试使用Html.DisplayFor(),但是它与model一起使用,只显示模型的属性,而不显示列表
我想达到某种形式
<% foreach (var item in Model) { %>
<tr>
<td>
<%:Html.Display("item")%>
</td>
<td>
<%:Html.Display("item.Value")%>
</td>
</tr>
<% } %>
您可以尝试在主视图中使用编辑器模板,该模板将为模型的每个项目呈现(假设您的模型是一个集合)。编辑器模板比显示模板更适合您的场景,因为您正在呈现允许编辑的文本框。因此,在语义上使用
EditorFor
比使用DisplayFor
更为正确:
<table>
<%= Html.EditorForModel() %>
</table>
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<YourAppName.Models.ViewModel>" %>
<tr>
<td>
<%: Model.Key %>
</td>
<td>
<%= Html.TextBoxFor(x => x.Value) %>
</td>
</tr>