I have a IList<Tag>
as a property named Tags
in my model. How do I name the files for display and editor templates to respect it when I call DisplayFor
or EditorFor
? Usage:
class MyModel
{
IList<Tag> Tags { get; protected set; }
}
<%= Html.EditorFor(t => t.Tags) %>
edit I know I can do this, but its not what I want to do.
<%= Html.EditorFor(t => t.Tags, "TagList") %>
Use the attribute [UIHint("Tags")] then create a display template called Tags.ascx in the DisplayTemplates folder.
class MyModel { [UIHint("Tags")] IList<Tag> Tags { get; protected set; } }
And in the file Tags.ascx
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Tag>>" %> <!-- put your Model code here ->
Works for me
I had the same problem today. Hope this helps:
forach(var tag in Tags) {
<%= Html.EditorFor( _ -> tag) %>
}
If you absolutely want to do sth. like
Html.EditorFor(mymodel=>mymodel.Tags)
Then you will have to:
Create a UserControl (TagList.ascx) and add an UIHint attribute to MyModel
class MyModel {
[UIHint("Taglist")]
IList<Tag> Tags {get; protected set;}
}
I had the same problem as you :-/ but found this useful post which gives you 4 different options to solve the problem :-):
http://blogs.msdn.com/b/stuartleeks/archive/2010/04/01/collections-and-asp-net-mvc-templated-helpers-part-4.aspx
This is also interesting (more or less same solution as one of the solutions in the previous link - but interesting):
http://weblogs.asp.net/rashid/archive/2010/02/09/asp-net-mvc-complex-object-modelmetadata-issue.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With