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

£可爱£侵袭症+ 提交于 2019-12-05 13:55:29

Start by decorating your view model with the [UIHint] attribute:

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):

@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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!