我试图在 Create
中显示一个类对象 View ,其中属性是 ICollection<string>
.
例如……
namespace StackOverflow.Entities
{
public class Question
{
public int Id { get; set; }
....
public ICollection<string> Tags { get; set; }
}
}
如果 View 类似于 StackOverflow 的“提问”页面,其中 Tags
html 元素是单个 input box
.. 我不确定如何在 ASP.NET MVC3 View 中做到这一点?
有什么想法吗?
我尝试使用 EditorFor
但浏览器中什么也没有显示,因为它不确定如何呈现字符串集合。
最佳答案
首先用 [UIHint]
装饰你的 View 模型属性:
public class Question
{
public int Id { get; set; }
[UIHint("tags")]
public ICollection<string> Tags { get; set; }
}
然后在主视图中:
@model StackOverflow.Entities.Question
@Html.EditorFor(x => x.Tags)
然后您可以编写自定义编辑器模板 (~/Views/Shared/EditorTemplates/tags.cshtml
):
@model ICollection<string>
@Html.TextBox("", string.Join(",", Model))
或者如果你不喜欢装饰,你也可以直接在 View 中指定要用于给定属性的编辑器模板:
@model StackOverflow.Entities.Question
@Html.EditorFor(x => x.Tags, "tags")
关于c# - 尝试在 ASP.NET MVC3 View 中将 EditorFor 与 ICollection<string> 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7635555/