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

发布于 2024-10-03 07:22:47 字数 539 浏览 3 评论 0原文

这样我就了解了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぇ气 2024-10-10 07:22:47

AFAIK,这两个都是不正确的。当您指定不是 RouteValueDictionary 的对象(通常是匿名对象)时,它会通过对该对象的属性使用反射将属性/值对传输到字典中。在这两种情况下,您都不会获得一个转换为 RouteValueDictionary 的对象,其中包含带有关联值的 id 键。在第二种情况下,我认为你甚至没有合法的语法它不会正确映射到给定默认路由的 id 参数。您需要的语法是:

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

您正在创建一个带有 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 syntax it won't map correctly onto the id parameter given the default routes. 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. This will result in a RouteValueDictionary with an id key that maps onto the value of Model.ProductId.

夢归不見 2024-10-10 07:22:47

您可能想将其他值发送到控制器操作

示例:

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

您的控制操作是这样的

Public ActionResult Edit(int id, int myotherValue);

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);
り繁华旳梦境 2024-10-10 07:22:47

B) 语法 new { id = 1, name = "11" } 表示“创建匿名对象”。在构建过程中,编译器会找到所有此类用法,并在程序集中创建具有不可读名称的类,如下所示:

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

然后所有引用都更改为如下代码 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:

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.

A) So you see, that Model.ProductId is int, and new { Model.ProductId } is instance of some type, generated by compiler.

You could examine this using Reflector. More info about Anonymous Types

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文