为什么产生DataBaseManagerController表头在ASP使用lambda前pression MVC 4生成 [英] Why in generated DataBaseManagerController table header is generated using lambda expression in ASP MVC 4

查看:206
本文介绍了为什么产生DataBaseManagerController表头在ASP使用lambda前pression MVC 4生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经生成 StoreManagerController 我的解决方案。的Visual Studio 2013等产生
StoreManager / Index.cshtml视图

I have generated StoreManagerController for my solution. Visual Studio 2013 generated such StoreManager/Index.cshtml view

@model IEnumerable<MvcMusicStore.Models.Album>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Genre.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Artist.Name)
        </th>
        <th>
           @* @Html.DisplayNameFor(model => model.Title)*@
            TITLE
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Genre.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Artist.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.AlbumId }) |
            @Html.ActionLink("Details", "Details", new { id=item.AlbumId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.AlbumId })
        </td>
    </tr>
}

</table>

问题:

1.为什么表头是由产生C $ C>或至少 @ Html.DisplayNameFor(model.Title)

1.Why table header is generated by : @Html.DisplayNameFor(model => model.Title) not just Title or at least @Html.DisplayNameFor(model.Title).

2.Is DisplayNameFor 方法返回的字段的名称,所以模型=> model.x它返回x?这是类似反映在Java中?

2.Is DisplayNameFor method returns name of the field, so for model => model.x it returns x? Is this something like reflection in Java?

3.Is必要在这里的lambda前pression?

3.Is lambda expression necessary here?

结果:

Result:

推荐答案

您所有的问题基本上都是相同的一个变化。 DisplayNameFor 被用于为财产返还面向用户的名称。在最基本的情况下它只是要返回属性名称本身,这不是没有在EX pression实际上指定属性名称工作,所以这是合理的问,为什么不硬code中的名称。当你的财产指定显示名称的真正好处,加入这样的:

All your questions are basically a variation on the same one. DisplayNameFor is used to return the user-facing name for a property. In the most basic of scenarios it's just going to return the property name itself, which wouldn't work without actually specifying the property name in the expression, so it's reasonable to ask, "why not just hard-code the name". The real benefit comes when you specify a display name for the property by adding something like:

[Display(Name = "My Awesome Title")]
public string Title { get; set; }

现在,它返回我真棒标题,而不是标题,如果以后决定更改显示名称为我的超级真棒标题,你改变它只有一次的地方,到处都已经二手 DisplayNameFor ,该值会自动更新。

Now, it returns "My Awesome Title", instead of "Title", and if you later decide to change that display name to "My Super Awesome Title", you change it in just once place, and everywhere you've used DisplayNameFor, the value is automatically updated.

的前pression是必要的较低级别的原因。如果你只是通过 model.Title ,你会被传递将是属性的值。这位前pression用来传递属性,本身的,其中 DisplayNameFor 然后使用上反射来获取相应的显示名称。

The expression is necessary for a lower-level reason. If you just passed model.Title, what would be passed would be the value of the property. The expression serves to pass the property, itself, which DisplayNameFor then uses reflection on to get the appropriate display name.

这篇关于为什么产生DataBaseManagerController表头在ASP使用lambda前pression MVC 4生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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