[英]Trying to use EditorFor with an ICollection<string> in an ASP.NET MVC3 View?
I'm trying to display a class object in a Create
view, where a property is an ICollection<string>
. 我正在尝试在
Create
视图中显示一个类对象,其中一个属性是ICollection<string>
。
For example... 例如...
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 a question' page, where the Tags
html element is a single input box
.. I'm not sure how I could do that in an ASP.NET MVC3 view? 如果视图就像StackOverflow'问一个问题'页面,那么
Tags
html元素是一个单独的input box
..我不知道如何在ASP.NET MVC3视图中做到这一点?
Any ideas? 有任何想法吗?
I tried using EditorFor
but nothing was displayed in the browser, because it's not sure how to render a collection of strings. 我尝试使用
EditorFor
但浏览器中没有显示任何内容,因为它不确定如何呈现字符串集合。
Start by decorating your view model with the [UIHint]
attribute: 首先使用
[UIHint]
属性修饰视图模型:
public class Question
{
public int Id { get; set; }
[UIHint("tags")]
public ICollection<string> Tags { get; set; }
}
and then in the main view: 然后在主视图中:
@model StackOverflow.Entities.Question
@Html.EditorFor(x => x.Tags)
and then you could write a custom editor template ( ~/Views/Shared/EditorTemplates/tags.cshtml
): 然后你可以编写一个自定义编辑器模板(
~/Views/Shared/EditorTemplates/tags.cshtml
):
@model ICollection<string>
@Html.TextBox("", string.Join(",", Model))
or if you don't like decorating, you could also specify the editor template to be used for the given property directly in the view: 或者如果您不喜欢装饰,您还可以直接在视图中指定要用于给定属性的编辑器模板:
@model StackOverflow.Entities.Question
@Html.EditorFor(x => x.Tags, "tags")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.