@ Html.EditorFor(M = GT;米)的MVC lambda语法 [英] @Html.EditorFor(m => m) lambda syntax in MVC

查看:213
本文介绍了@ Html.EditorFor(M = GT;米)的MVC lambda语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚学习C#和MVC,并试图了解一些例子。

I'm just learning C# and MVC, and trying to understand some examples.

@Html.EditorFor(m => m)

后来我想通了,'=>'是的lambda算子,它意思类似M使得m。这并不真正使任何意义,我。为什么不通过并购?

Eventually I figured out that '=>' is the lambda operator, and that it means something like "m such that m". That doesn't really make any sense to me. Why not just pass in m?

另外,我不认为在我工作的任何视图定义的米模型定义,据称这就是这种方法正在回升。这是如何工作的?

Also, I don't see m defined in any view that I'm working with. Model is defined, and allegedly that's what this method is picking up. How does that work?

最后,我看着定义Html.EditorFor,并没有看到任何超载的传递只是一个单一的参数。在这里的定义是这样的语法?
http://msdn.microsoft.com/en-us/library/ee834942.aspx

Finally, I looked at the definition for Html.EditorFor, and don't see any overload for passing in just a single parameter. Where is this syntax defined?? http://msdn.microsoft.com/en-us/library/ee834942.aspx

推荐答案

让我们打破这通过检查方法签名:

Let's break this down by examining the method signature:

MvcHtmlString EditorFor<TModel, TValue>(
    this HtmlHelper<TModel> html, 
    Expression<Func<TModel, TValue>> expression
)

这是使用扩展方法的语法,这意味着它加入了一个名为方法 EditorFor 的HtmlHelper ,这样你可以拨打电话 Html.EditorFor 。但是我们真正感兴趣的是第二个参数,表达式来; Func键<的TModel,TValue>> 。这是一个相当复杂的参数,但现在我们可以忽略一个事实,即它是一个表达式。因此简化了,让我们来看看:

This is using extension method syntax, which means it's adding a method named EditorFor to HtmlHelper such that you can make the call Html.EditorFor. But what we're really interested in is the second parameter, Expression<Func<TModel, TValue>>. That's a pretty complicated parameter, but for now we can ignore the fact that it's an Expression. So simplifying, let's examine:

Func<TModel, TValue> expression

这意味着该参数是的任何的方法,它有一个参数(类型的TModel ),并返回类型为 TValue 。你已经使用lambda表达式,这是(本质)的方法的更简洁的表示,但它有助于只是把它当作一个普通的方法。所以,你的lambda正在采取的模式并返回一个模型:

This means that the argument is any method that has one parameter (of type TModel) and the return type is TValue. You've been using lambdas, which is (essentially) a more concise representation of a method, but it's helpful to just think of it as an ordinary method. So you're lambda is taking a model and returning a model:

m => m

这不是那么有趣,所以我们把它比作你在哪里返回一个更加真实的场景物业关闭模式:

That's not as interesting, so let's compare it to a more realistic scenario where you're returning a property off the model:

m => m.MyStringProperty

现在,让我们与您在什么地方申报一个普通的静态方法比较吧:

Now let's compare it with an ordinary static method you've declared somewhere:

public static class MyStaticClass 
{
    public static string Foo(TModel model) 
    {
        return model.MyStringProperty;
    }
}



虽然真的在这里它不会是的TModel - 这将是不管你通过声明 @model 。现在,为了讨论的方便,你可以有,而不是用这种方法在你的 EditorFor 的调用:

Although really here it wouldn't be TModel -- it would be whatever you declared your model type via @model. Now, for the sake of discussion, you could have instead used this method in your invocation of EditorFor:

Html.EditorFor(MyStaticClass.Foo);



所以总结起来,lambda表达式的(大部分)仅几步之手一个普通的方法。因此,所有你正在做的是周围路过的方法。

So to sum up, lambdas are (for the most part) just a short hand for a regular method. So all you're doing is passing methods around.

在最后一个音符在这里,我们实际使用的表达式目录树,这意味着你实际上并没有传递方法,你传递表示方法的代码对象模型(表达式树)。这是本质上,只是用来找出你正在使用的属性名(因为通常拉姆达会更喜欢 M => m.MyProperty ,不只是 M =>,M )。这一切都是为了避免魔法字符串,你使用字符串(即myProperty的)是指属性名称。

The last note here is that we are actually using expression trees, which means you aren't actually passing the method, you're passing an object model (an expression tree) that represents the code of the method. This is, essentially, just used to figure out the property name you're using (because usually the lambda would be more like m => m.MyProperty, not merely m => m). This is all to avoid magic strings where you refer to the property name by using a string (i.e. "MyProperty").

这篇关于@ Html.EditorFor(M = GT;米)的MVC lambda语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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