I have a list of Key/Value pairs. basically its a List where ViewModel is a custom class of the form
public class ViewModel
{
public String Key { get; set; }
public String Value { get; set; }
}
In the View i would need to render Label and Textbox for Key and Value respectively.Im trying to use Html.DisplayFor() however it goes with model and only displays the properties of the model and not the list.
I would like to achieve something of the format
<% foreach (var item in Model) { %>
<tr>
<td>
<%:Html.Display("item")%>
</td>
<td>
<%:H开发者_Go百科tml.Display("item.Value")%>
</td>
</tr>
<% } %>
You could try using an editor template inside the main view which will be rendered for each item of the model (assuming your model is a collection). Editor templates are more suitable for your scenario than display templates because you are rendering text boxes which allow for editing. So it would be semantically more correct to use EditorFor
rather than DisplayFor
:
<table>
<%= Html.EditorForModel() %>
</table>
and then define an editor template for the view model (~/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>
精彩评论