How to create a generic MVC3 editor template?

前端 未结 1 875
野性不改
野性不改 2020-12-16 05:08

I\'m using the following code-snippet extensively in my model templates.

@Html.LabelFor(model => model.FirstName)
相关标签:
1条回答
  • 2020-12-16 05:41

    Is it possible to encapsulate this generically in an editor template so I can use Html.EditorFor(...) without resorting to a custom extension?

    Of course:

    ~/Views/Shared/EditorTemplates/Foo.cshtml:

    <div class="control-group">
        @Html.Label("")
        <div class="controls">
            @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "span3" })
            @Html.ValidationMessage("")
        </div>
    </div>
    

    and then:

    @Html.EditorFor(x => x.FirstName, "Foo")
    

    or:

    [UIHint("Foo")]
    pubilc string FirstName { get; set; }
    

    and then:

    @Html.EditorFor(x => x.FirstName)
    
    0 讨论(0)
提交回复
热议问题