简体   繁体   English

有关Html.ActionLink帮助器的问题-特别是对象routeValues参数的语法

[英]Question on the Html.ActionLink helper - Specifically the syntax for the object routeValues parameter

So I understand the use of the ActionLink HTML helper, as well as most of the parameters. 因此,我了解ActionLink HTML帮助器的使用以及大多数参数。 I am still in the process of wrapping my head around the complete range of uses for the object routeValues parameter. 我仍在全神贯注于对象routeValues参数的完整用法范围。

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: 但是,我的问题是,对于routeValues参数,您似乎可以将对象指定为ViewData字典中的现有对象:

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? A)相互使用的确定性原因是什么? 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 (ie var someVar = new SomeObject()), but what are you defining in the above example ( new { Model.ProductID }) and your specifying a property? B)我了解实例化一个新对象(即var someVar = new SomeObject()),但是您在上述示例(new {Model.ProductID))中定义了什么,并且您指定了一个属性?

Thanks, 谢谢,

You may want to send other values to the controller action 您可能希望将其他值发送到控制器操作

Example: 例:

Html.ActionLink("Some Text", "Edit", new { id=Model.ProductId, myotherValue=1 });

And your control action is something like this 而您的控制动作就是这样

Public ActionResult Edit(int id, int myotherValue);

AFAIK, both of those are incorrect. AFAIK,这两个都不正确。 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. 当您指定一个不是RouteValueDictionary的对象(通常是一个匿名对象)时,它将通过对对象属性的反射将属性/值对传输到字典中。 In neither case you use will you get an object that translates into a RouteValueDictionary containing an id key with an associated value. 无论使用哪种方式,都不会得到一个对象,该对象会转换为包含与相关值的id密钥的RouteValueDictionary。 In the second case, I don't think you even have legal syntax it won't map correctly onto the id parameter given the default routes. 在第二种情况下, 我不认为您具有合法的语法 即使 给定了默认路由,它也无法正确映射到id参数上。 The syntax that you want is: 您想要的语法是:

Html.ActionLink("Some Text", "Edit", new { id = Model.ProductId } );

You're creating an anonymous object with an id property whose value is Model.ProductId . 您正在创建一个id属性为Model.ProductId的匿名对象。 This will result in a RouteValueDictionary with an id key that maps onto the value of Model.ProductId . 这将导致一个带有id键的RouteValueDictionary,该键映射到Model.ProductId的值。

B) syntax new { id = 1, name = "11" } means "create anonymous object". B)语法new { id = 1, name = "11" }意思是“创建匿名对象”。 During build process compiler finds all such usages and creates classes with unreadable names in your assembly like this: 在构建过程中,编译器会找到所有此类用法,并在程序集中创建名称不可读的类,如下所示:

class AnonymousType`1
{
    int id {get; private set};
    int name {get; private set};
}

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. 然后所有引用都变为new <>AnonymousType`1{ id = 1, name = "11" } 这样的代码,这只是对其工作方式的描述,生成的代码稍微复杂一些。

A) So you see, that Model.ProductId is int, and new { Model.ProductId } is instance of some type, generated by compiler. A)因此,您看到Model.ProductId为int,而new { Model.ProductId }是由编译器生成的某种类型的实例。

You could examine this using Reflector. 您可以使用Reflector进行检查。 More info about Anonymous Types 有关匿名类型的更多信息

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

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