尝试在ASP.NET MVC3视图中使用带有ICollection 的EditorFor?
我正在尝试在Create
视图中显示一个类对象,其中一个属性是ICollection
。
例如…
namespace StackOverflow.Entities { public class Question { public int Id { get; set; } .... public ICollection Tags { get; set; } } }
如果视图就像StackOverflow’问一个问题’页面,那么Tags
html元素是一个单独的input box
..我不知道如何在ASP.NET MVC3视图中做到这一点?
有任何想法吗?
我尝试使用EditorFor
但浏览器中没有显示任何内容,因为它不确定如何呈现字符串集合。
首先使用[UIHint]
属性修饰视图模型:
public class Question { public int Id { get; set; } [UIHint("tags")] public ICollection Tags { get; set; } }
然后在主视图中:
@model StackOverflow.Entities.Question @Html.EditorFor(x => x.Tags)
然后你可以编写一个自定义编辑器模板( ~/Views/Shared/EditorTemplates/tags.cshtml
):
@model ICollection @Html.TextBox("", string.Join(",", Model))
或者如果您不喜欢装饰,您还可以直接在视图中指定要用于给定属性的编辑器模板:
@model StackOverflow.Entities.Question @Html.EditorFor(x => x.Tags, "tags")