Trying to use EditorFor with ICollection <string> in an ASP.NET MVC3 view?
I am trying to show a class object in the Create view, where the property is ICollection<string> .
For instance...
namespace StackOverflow.Entities { public class Question { public int Id { get; set; } .... public ICollection<string> Tags { get; set; } } } and if the view was like a StackOverflow "ask question" page, where the Tags html element is one input box . I'm not sure how to do this in ASP.NET MVC3 view
Any ideas?
I tried using EditorFor , but nothing was displayed in the browser because it is not sure how to display the collection of strings.
+4
1 answer
Start by decorating your model with the [UIHint] attribute:
public class Question { public int Id { get; set; } [UIHint("tags")] public ICollection<string> Tags { get; set; } } and then on the main screen:
@model StackOverflow.Entities.Question @Html.EditorFor(x => x.Tags) and then you can write your own editor template ( ~/Views/Shared/EditorTemplates/tags.cshtml ):
@model ICollection<string> @Html.TextBox("", string.Join(",", Model)) or if you do not like decoration, you can also specify an editor template that will be used for this property directly in the view:
@model StackOverflow.Entities.Question @Html.EditorFor(x => x.Tags, "tags") +6