如何创建一个通用的MVC3编辑模板? [英] How to create a generic MVC3 editor template?

查看:145
本文介绍了如何创建一个通用的MVC3编辑模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code-广泛片段在我的模型模板。

 < D​​IV CLASS =控制组>
    @ Html.LabelFor(型号=> model.FirstName)
    < D​​IV CLASS =控制>
        @ Html.TextBoxFor(型号=> model.FirstName,新{@class =span3})
        @ Html.ValidationMessageFor(型号=> model.FirstName)
    < / DIV>
< / DIV>

是否有可能在一般的编辑模板的,所以我可以使用Html.EditorFor(...)这个封装的诉诸自定义扩展?


解决方案

  

是否有可能在编辑器中的模板一般这种封装
  所以我可以使用Html.EditorFor(...)而不是诉诸自定义
  扩展?


当然:

〜/查看/共享/ EditorTemplates / Foo.cshtml

 < D​​IV CLASS =控制组>
    @ Html.Label()
    < D​​IV CLASS =控制>
        @ Html.TextBox(,ViewData.TemplateInfo.FormattedModelValue,新{@class =span3})
        @ Html.ValidationMessage()
    < / DIV>
< / DIV>

和则:

  @ Html.EditorFor(X => x.FirstName,富)

  [UIHint(富)]
pubilc字符串名字{获得;组; }

和则:

  @ Html.EditorFor(X => x.FirstName)

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

<div class="control-group">
    @Html.LabelFor(model => model.FirstName)
    <div class="controls">
        @Html.TextBoxFor(model => model.FirstName, new { @class = "span3" })
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>
</div>

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

解决方案

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)

这篇关于如何创建一个通用的MVC3编辑模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆