为什么不使用Html.EditorForModel() [英] Why not use Html.EditorForModel()

查看:694
本文介绍了为什么不使用Html.EditorForModel()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我刚刚发现有关 EditorForModel MVC中,我想知道我什么时候应该使用代替 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!

推荐答案

由于接受的答案是唯一的链接的答案(并除去),我想我其实回答的布拉德威尔逊的博客:的 ASP.NET MVC 2模板,第1部分:介绍

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.

该模型前pressions是其当前模型操作简单帮手。行DisplayForModel()等效于DisplayFor(型号=>模型)。

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

TL; DR 可以假设为 EditorFor(型号=>模型)相同的想法 EditorForModel( );这些辅助方法达到同样的事情。 EditorForModel()假设模型前pression是 @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)

这篇关于为什么不使用Html.EditorForModel()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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