繁体   English   中英

尝试在ASP.NET MVC3视图中使用带有ICollection <string>的EditorFor?

[英]Trying to use EditorFor with an ICollection<string> in an ASP.NET MVC3 View?

我正在尝试在Create视图中显示一个类对象,其中一个属性是ICollection<string>

例如...

namespace StackOverflow.Entities
{
    public class Question
    {
        public int Id { get; set; }
        ....
        public ICollection<string> 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<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))

或者如果您不喜欢装饰,您还可以直接在视图中指定要用于给定属性的编辑器模板:

@model StackOverflow.Entities.Question
@Html.EditorFor(x => x.Tags, "tags")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM