关于 Html.ActionLink 帮助程序的问题 - 特别是对象 routeValues 参数的语法
这样我就了解了 ActionLink HTML helper 的使用,以及大部分参数。我仍在思考对象routeValues 参数的完整用途。
然而,我的问题是,对于routeValues参数,您似乎可以将对象指定为ViewData字典中的现有对象:
Html.ActionLink("Some Text", "Edit", Model.ProductId);
或者您可以使用对象初始化语法:
Html.ActionLink("Some Text", "Edit", new { Model.ProductId });
我的2个问题是
A)使用一个的定义原因是什么超过另一个?我很难理解为什么您必须初始化一个新的属性,因为您已经在模型中拥有具有正确值的属性。
B)我理解实例化一个新对象(即 var someVar = new SomeObject()),但是您在上面的示例中定义了什么( new { Model.ProductID } )并指定一个属性?
谢谢,
So I understand the use of the ActionLink HTML helper, as well as most of the parameters. I am still in the process of wrapping my head around the complete range of uses for the object routeValues parameter.
However, my question is that it seems for the routeValues parameter you could specify the object as either the existing object from the ViewData dictionary:
Html.ActionLink("Some Text", "Edit", Model.ProductId);
or you can use object initializing syntax:
Html.ActionLink("Some Text", "Edit", new { Model.ProductId });
My 2 questions, are
A) what is the defining reason to use one over another? I have a hard time understanding why you have to initialize a new one since you already have the property with the correct value in the Model.
B) I understand instantiating a new object (i.e. var someVar = new SomeObject()), but what are you defining in the above example ( new { Model.ProductID }) and your specifying a property?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK,这两个都是不正确的。当您指定不是 RouteValueDictionary 的对象(通常是匿名对象)时,它会通过对该对象的属性使用反射将属性/值对传输到字典中。在这两种情况下,您都不会获得一个转换为 RouteValueDictionary 的对象,其中包含带有关联值的
id
键。在第二种情况下,我认为你甚至没有合法的语法它不会正确映射到给定默认路由的 id 参数。您需要的语法是:您正在创建一个带有
id
属性的匿名对象,其值为Model.ProductId
。这将生成一个带有id
键的 RouteValueDictionary,该键映射到Model.ProductId
的值。AFAIK, both of those are incorrect. When you specify an object that isn't a RouteValueDictionary (typically an anonymous object), it transfers the property/value pairs into the dictionary by using reflection over the properties of the object. In neither case you use will you get an object that translates into a RouteValueDictionary containing an
id
key with an associated value. In the second case,I don't think you even have legal syntaxit won't map correctly onto the id parameter given the default routes. The syntax that you want is:You're creating an anonymous object with an
id
property whose value isModel.ProductId
. This will result in a RouteValueDictionary with anid
key that maps onto the value ofModel.ProductId
.您可能想将其他值发送到控制器操作
示例:
您的控制操作是这样的
You may want to send other values to the controller action
Example:
And your control action is something like this
B) 语法
new { id = 1, name = "11" }
表示“创建匿名对象”。在构建过程中,编译器会找到所有此类用法,并在程序集中创建具有不可读名称的类,如下所示:然后所有引用都更改为如下代码
new <>AnonymousType`1{ id = 1, name = "11" }
它只是对其工作原理的描述,生成的代码稍微复杂一些。
A)所以你会看到,
Model.ProductId
是 int,而new { Model.ProductId }
是某种类型的实例,由编译器生成。您可以使用 Reflector 来检查这一点。有关匿名类型的详细信息
B) syntax
new { id = 1, name = "11" }
means "create anonymous object". During build process compiler finds all such usages and creates classes with unreadable names in your assembly like this:then all references change to code like this
new <>AnonymousType`1{ id = 1, name = "11" }
It is just a description of how it works, generated code is a little more complex.
A) So you see, that
Model.ProductId
is int, andnew { Model.ProductId }
is instance of some type, generated by compiler.You could examine this using Reflector. More info about Anonymous Types