开发者

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>.

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?

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.


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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜