4

Createプロパティが であるビューにクラス オブジェクトを表示しようとしていますICollection<string>

例えば...

namespace StackOverflow.Entities
{
    public class Question
    {
        public int Id { get; set; }
        ....
        public ICollection<string> Tags { get; set; }
    }
}

ビューがStackOverflowの「質問する」ページのようで、Tagshtml要素が1つである場合input box.. ASP.NET MVC3ビューでそれを行う方法がわかりませんか?

何か案は?

使ってみEditorForたのですが、文字列のコレクションをレンダリングする方法がわからないため、ブラウザーに何も表示されませんでした。

4

1 に答える 1

6

ビューモデルを次の[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")
于 2011-10-03T21:37:17.987 に答える