为什么不使用 Html.EditorForModel()

发布于 2024-11-18 01:25:48 字数 177 浏览 1 评论 0原文

好的,我刚刚在 MVC 中发现了 EditorForModel,我想知道什么时候应该在我的每个属性上使用它而不是 EditorFor?为什么当我添加强类型视图时,它不使用它并在每个属性上构建 EditorFor

我迟到了...但谢谢你的信息!

Ok I just discovered about the EditorForModel in MVC and I want to know when I should use this instead of an EditorFor on each of my property? And why does when I add a strongly typed view it does not use this and build an EditorFor on every property?

I'm late on this... but thanks for the info!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柳若烟 2024-11-25 01:25:48

由于接受的答案是仅链接的答案(并被删除),我想我实际上会回答源自 Brad 的问题Wilson 的博客:ASP.NET MVC 2 模板,第 1 部分:简介

模型表达式是对当前模型进行操作的简单助手。 DisplayForModel() 行等效于 DisplayFor(model => model)。

TL;DR 对于 EditorFor(model => model)EditorForModel() 可以假设相同的想法;这些辅助方法实现了相同的目标。 EditorForModel() 假定模型表达式是传递给视图的@model

以以下模型和视图为例:

public class Person
{
    public string Name {get; set;}
    public Address MailingAddress {get; set;}
}

public class Address
{
    public String Street {get; set;}
    public String City {get; set;}
    public String State {get; set;}
}

Create.cshtml

@model MyNamespace.Models.Person

/* So, you need an Editor for the Person model? */
@Html.EditorForModel()
/*the above is equivalent to @Html.EditorFor(model => model) */

/* you need to specify the Address property that the editor accepts? */
@Html.EditorFor(model => model.MailingAddress)

Since the accepted answer is a link-only answer (and was removed), I thought I'd actually answer the question derived from Brad Wilson's Blog: ASP.NET MVC 2 Templates, Part 1: Introduction.

The model expressions are simple helpers which operate on the current model. The line DisplayForModel() is equivalent to DisplayFor(model => model).

TL;DR the same idea can be assumed for EditorFor(model => model) and EditorForModel(); these helper methods achieve the same thing. EditorForModel() assumes the model expression is the @model that was passed to the view.

Take the following models and view for example:

public class Person
{
    public string Name {get; set;}
    public Address MailingAddress {get; set;}
}

public class Address
{
    public String Street {get; set;}
    public String City {get; set;}
    public String State {get; set;}
}

Create.cshtml:

@model MyNamespace.Models.Person

/* So, you need an Editor for the Person model? */
@Html.EditorForModel()
/*the above is equivalent to @Html.EditorFor(model => model) */

/* you need to specify the Address property that the editor accepts? */
@Html.EditorFor(model => model.MailingAddress)
眼睛会笑 2024-11-25 01:25:48

您应该尽可能使用它,但有时您需要单独的 Html.EditorFor 使用的可定制性。

至于为什么内置模板不使用它,主要是因为它们通常很愚蠢,而且还因为,如果我记得的话,它们需要将元素(如表格行等)包裹在每个 Html 周围。编辑器

You should use it when possible, but sometimes you will need the customizability of individual Html.EditorFor uses.

As for why the built-in templates don't use it, that's mainly because they are silly in general, but also because, if I recall, they need to wrap elements (like table rows etc.) around each Html.EditorFor.

失与倦" 2024-11-25 01:25:48

@Html.EditorForModel() ??并放弃写自己观点的乐趣? 微笑

除了有趣之外,将其作为一种习惯也是相当冒险的。考虑以下常见场景 - 您的数据库中的客户表中有一个 bool 变量 IsMale。显然你不想要默认版本(带有复选框的 IsMale) - 你可能想要一些更友好的东西,比如 {select, Options .... , /select} 标签,对吗?这就是视图真正开始发挥作用的地方。这就是定制。每个视图都有点不同。您拥有 RAZOR 引擎,请最大限度地利用它!在您看来,您可以覆盖任何内容,甚至可以手动键入您自己的整个 HTML 代码块。

@Html.EditorForModel() ?? And give up the fun of writing your own view? smile

Besides the fun, doing so as a habit is rather dicey. Consider the following common scenario - you have a bool variable say IsMale in your database in your customer table. Well obviously you don't want the default version (IsMale with a check-box) - you probably want something a bit more friendly, say a {select, Options .... , /select} tags, right? that's where the view really starts kicking in. That's the customization. Every view is a little different. You have the RAZOR engine, exploit it to the max! In your view you can override anything, or even manually type an entire chunk of HTML code of your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文