简体   繁体   English

Asp.net剃刀视图 - lambda表达式输入

[英]Asp.net razor view - lambda expression inputs

I am doing this MVC tutorial and I don't understand the input parameter in the lambda expression inside @Html.DisplayNameFor method. 我正在做这个 MVC教程,我不理解@Html.DisplayNameFor方法内的lambda表达式中的输入参数。 The image below has 下面的图片有

@Html.DisplayNameFor(model=> model.Title) @ Html.DisplayNameFor(model => model.Title)

but it works fine even if I change it to 但即使我改成它也能正常工作

@Html.DisplayNameFor(something => something.Title) @ Html.DisplayNameFor(something => something.Title)

So my question is how are the variables model or something getting declared and how the values are being populated? 所以我的问题是如何变量modelsomething被声明以及如何填充值? All I see is they are simply supplied as inputs to lambda expression. 我只看到它们只是作为lambda表达式的输入提供的。

电影控制器的索引视图

Have a look at the actual signature of the method ( from MSDN documentation ) 看看方法的实际签名( 来自MSDN文档

public static MvcHtmlString DisplayFor<TModel, TValue>(
    this HtmlHelper<TModel> html,
    Expression<Func<TModel, TValue>> expression,
    string templateName
)

DisplayFor is actually an extension method that will be available on HtmlHelper<TModel> instances, where TModel is the type of your model, as defined by the type of that is given through the @Model directive. DisplayFor实际上是一个扩展方法,可以在HtmlHelper<TModel>实例上使用,其中TModel是模型的类型,由通过@Model指令给出的类型定义。

As you can see, the second argument is an Expression<Func<TModel, TValue>> . 如您所见,第二个参数是Expression<Func<TModel, TValue>> This means that, in a call such as this: @Html.DisplayNameFor(x => x.Foo) , x will always be the same type as the one you declared using @model , regardless of the name you use. 这意味着,在这样的调用中: @Html.DisplayNameFor(x => x.Foo)x将始终与您使用@model声明的类型相同,无论您使用何种名称。

Now, you question was: how do these values get populated ? 现在,您的疑问是:这些值如何填充? Well, since you have declared that you want a model of type IEnumerable<MvcMovie.Models.Movie> , you can now do something like this in your code behind 好吧,因为你已经声明你想要一个IEnumerable<MvcMovie.Models.Movie>类型的模型,你现在可以在你的代码中执行类似的操作

public ActionResult MoviesView()
{
    var model = new List<MvcMovie.Models.Movie>()
    { 
        new Movie("Casablanca"),
        new Movie("Fight Club"),
        new Movie("Finding Nemo")
    };

    return View(model);
}

This will be how the values are "populated". 这将是值“填充”的方式。 The Expression<Func<TModel, TValue>> expects a IEnumerable<MvcMovie.Models.Movie> model, and, with this call, you have provided it. Expression<Func<TModel, TValue>>需要IEnumerable<MvcMovie.Models.Movie>模型,并且通过此调用,您已经提供了它。

ASP.NET MVC Html Helpers are designed such that they know they're always working on an instance of your model. ASP.NET MVC Html Helpers的设计使他们知道他们一直在处理模型的实例。 See this MSDN doc where it describes HtmlHelper working on , which represents a generic model. 请参阅此MSDN文档 ,其中介绍了HtmlHelper的工作原理,它代表了一个通用模型。 Since the lambda is always expecting an input of some model property, it doesn't actually matter what you name the input. 由于lambda总是期望输入某些模型属性,因此输入的名称并不重要。

Think if the function was written like this: 想一想这个函数是这样编写的:

public string DisplayNameFor(string textToDisplay)
{
     displayStuff();
}

Which you could call as: 您可以称之为:

DisplayNameFor(model.Title);

or equivalently: 或等效地:

DisplayNameFor(something.Title);

DisplayNameFor() doesn't really care what the input is named, just that it's a string. DisplayNameFor()并不关心输入的名称,只是它是一个字符串。 Html helpers work in a similar manner by expecting to be called with a model instance. 期望通过模型实例调用Html帮助程序以类似的方式工作。

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

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