简体   繁体   English

ASP.NET MVC 3:字段的编辑器模板

[英]ASP.NET MVC 3: Editor templates for fields

<tr>
   <td>
        @Html.Label("Notes") </td><td> @Html.TextArea("Notes")
   </td></tr>
   <tr><td>
        @Html.Label("Action Date")</td><td> @Html.TextBoxFor(m => m.Due, new { @class = "dateTimePicker" })</td><td>
        @Html.ValidationMessageFor(m => m.Due)
   </td></tr>

Anyway I can create a template that will take any @HTML.Label , @Html.Textbox etc etc, and their for counterparts and place them into a table properly? 无论如何,我可以创建一个模板,将采取任何@HTML.Label@Html.Textbox等等等等,他们for同行,将它们放入一个表是否正确? without me having to polute all my views with table markups. 无需我用表标记污染所有视图。

You most certainly can. 您当然可以。 I have pieced together a quick sample based upon your sample code: 我根据您的示例代码拼凑了一个快速示例:

Given a basic model: 给定一个基本模型:

public class Foo
{
    public string Notes { get; set; }

    [Required]
    [DisplayName("Action Date")]
    public DateTime Due { get; set; }
}

And this simple controller: 而这个简单的控制器:

var model = new Foo { Notes = "Some Note String", Due = System.DateTime.Now };
return View(model);

You could call an editor template from your view: 您可以从视图中调用编辑器模板:

 @Html.Editor("Editor", "Foo", Model)

Given this template: 给定此模板:

@model StackExamples.Models.Foo

<table>
    <tr>
        <td>
            @Html.LabelFor(x => x.Notes)
        </td>
        <td>
            @Html.TextAreaFor(x=>x.Notes)
        </td>
    </tr>
    <tr>
        <td>
            @Html.LabelFor(x=>x.Due)
        </td>
        <td>
            @Html.TextBoxFor(m => m.Due, new { @class = "dateTimePicker" })
        </td>
        <td>
            @Html.ValidationMessageFor(m => m.Due)
        </td>
    </tr>
</table>

And have your output rendered as desired without the additional table markup in your views. 并按需要渲染输出,而视图中没有其他表标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM