ASP.NET MVC - 你能解释一下这些HtmlHelper语法吗? [英] ASP.NET MVC - Can you explain these HtmlHelper syntaxes?

查看:59
本文介绍了ASP.NET MVC - 你能解释一下这些HtmlHelper语法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




恐怕我无法理解以下



1.



我在视图中,这是我的模型类型声明



 @ model IEnumerable< BeerCupMVC.Models.SomeObject> 





请考虑该行



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





它显示属性SomeObjectName的DisplayName注释,但为什么我们放模型,为什么它会起作用呢?



 @ Html.DisplayNameFor(xyz = >  xyz.SomeObjectName)





2.

 @ foreach( var  item 模型中的class =code-keyword> {
< tr>
< td>
@ Html.DisplayFor(modelItem = > 项目)
< / td >
< td>
@ Html.ActionLink( 编辑 编辑 new {id = item.SomeObjectID})|
@ Html.ActionLink( 详细信息 详细信息 new {id = item.SomeObjectID})|
@ Html.ActionLink( 删除 删除 new {id = item.SomeObjectID})
< / td >
< / tr >
}





我可以理解循环内容但是如何解释这种语法中的modelItem? div class =h2_lin>解决方案

这是一个lambda表达式。当表达式适用于某种类型时,它的格式为



(x => ...)



x是任意的,它可以是你想要的任何东西,它实际上是表达式所处类型的临时变量。因此,对于;



 List< string> myData =  new  List< string>(); 
myData.Where(abc = > abc!= 你好);





我们在=左边的Where中说了什么;是abc是一个字符串类型的变量,而右边的=>是我们要评估的表达式,所以abc中的数据不是Hello。然后where将遍历列表中的每个实例,加载那个实例进入变量abc,然后检查是否abc!=你好。



所以对你所谓的变量没什么意义,它可能是 model,m或者甚至



 @ Html.DisplayNameFor(supercalifragilisticexpialidocious => supercalifragilisticexpialidocious.SomeObjectName)





人们倾向于选择模型或m,因为它明确了函数的作用。只是不要将模型与@model混淆视图顶部的指令,或模型属性:)


您需要学习lambda表达式。



1. model => model.SomeObjectName



在上面的代码中,模型是指

 @ model IEnumerable< BeerCupMVC.Models.SomeObject> 

在你的情况下是SomeObject。所以这对你来说很重要。





2. @ Html.DisplayFor(modelItem => item)





上面lambda中的modelItem是无关紧要的,这意味着你传递一个参数但没有使用它而你正在研究var项目。 />
一般来说,输入参数名称是无关紧要的,您可以使用任何名称但它必须来自集合或对象。



希望这有帮助。


Hi
I'm afraid I can't understand the following

1.

I'm in a view and this is my model type declaration

@model IEnumerable<BeerCupMVC.Models.SomeObject>



Please consider the line

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



It displays the DisplayName annotation of the property SomeObjectName but why do we put "model" and why does it works if we do the following?

@Html.DisplayNameFor(xyz => xyz.SomeObjectName)



2.

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



It's ok for me to understand the loop stuff but how to explain the "modelItem" in this syntax?

解决方案

That's a lambda expression. When an expression works on a type it is written in the format of

(x => ...)

"x" is arbitrary, it can be anything you want, it is effectively a temporary variable of the type that the expression works on. So for;

List<string> myData = new List<string>();
myData.Where (abc => abc != "Hello);



What we're saying in the "Where" left of the "=>" is that abc is a variable of type string, and right of the => is the expression we want to evaluation, so where the data in abc is not Hello. The Where will then loop through each instance in the list, load that instance into the variable abc, and then check if abc != "Hello".

So there is no significance to what you call the variable, it could be "model", "m" or even

@Html.DisplayNameFor(supercalifragilisticexpialidocious => supercalifragilisticexpialidocious.SomeObjectName)



People tend to chose model or m as it makes it obvious what the function is acting on. Just don't confuse that "model" with the "@model" directive at the top of the view, or the "Model" property :)


You need to learn lambda expression.

1. model => model.SomeObjectName

In the above code, model refers to the

@model IEnumerable<BeerCupMVC.Models.SomeObject>

which is SomeObject in your case. So this is significant in your case.


2. @Html.DisplayFor(modelItem => item)


The modelItem in the above lambda is insignificant which means you are passing a parameter but not using it and you are working on the var item rather.
In general, the input parameter name is irrelevant , you could use any name but it has to come from a collection or an object.

Hope this helps.


这篇关于ASP.NET MVC - 你能解释一下这些HtmlHelper语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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