将 RouteValues 字典传递给 ActionLink

发布于 2024-08-25 00:29:24 字数 1248 浏览 6 评论 0 原文

所有,

开始掌握 ASP.NET MVC。到目前为止,一切都很好,但这一个有点疯狂。

我有一个视图模型,其中包含超链接的属性字典,如下使用:

menu = model variable

Html.ActionLink(Html.Encode(menu.Name), Html.Encode(menu.Action), Html.Encode(menu.Controller), menu.Attributes, null)

问题是“menu.Attributes”的位置需要以下形式的对象:

new  { Name = "Fred", Age=24 }

据我所知,这个匿名对象实际上无论如何都是通过反射转换为字典的,但是你不能首先将字典传递给它!

为链接生成的 Html 仅显示字典类型。

我到底该如何解决这个问题?重点是它的一般性和控制器之前可以设置 menu.Attributes...

基于下面的帖子,我尝试了以下操作:

Html.ActionLink(Html.Encode(menu.Name), Html.Encode(menu.Action), Html.Encode(menu.Controller), new RouteValueDictionary(menu.Attributes), new Dictionary<string,object>())

但这仍然不起作用(我猜代码在内部调用了通用方法需要对象吗?)。上面的内容(以及我将字典传递给第四个参数的原始解决方案会生成与此类似的 HTML:

<a href="/EditRole?Comparer=System.Collections.Generic.GenericEqualityComparer%601%5BSystem.String%5D&amp;Count=1&amp;Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.String%5D&amp;Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.String%5D">EditDocumentRoles</a>

即它使用反射并完全错误地解决问题......

All,

Getting to grips with ASP.NET MVC. So far, so good, but this one is a little nuts.

I have a view model that contains a dictionary of attributes for a hyperlink, used like this:

menu = model variable

Html.ActionLink(Html.Encode(menu.Name), Html.Encode(menu.Action), Html.Encode(menu.Controller), menu.Attributes, null)

The problem is the position of "menu.Attributes" expects an object in the form:

new  { Name = "Fred", Age=24 }

From what I can tell, this anonymous object is actually converted to a dictionary via reflection anyway BUT you can't pass a dictionary to it in the first place!!!

The Html generated for the link simply shows the dictionary type.

How on earth do I get round this? The whole point is that its general and the controller can have set the menu.Attributes previously....

Based on a post below I tried the following:

Html.ActionLink(Html.Encode(menu.Name), Html.Encode(menu.Action), Html.Encode(menu.Controller), new RouteValueDictionary(menu.Attributes), new Dictionary<string,object>())

but this still doesn't work (I guess the code internally calls the generic method that takes objects?). The above (and my original solution of passing a dictionary to the 4th paramater produces a HTML similar to this:

<a href="/EditRole?Comparer=System.Collections.Generic.GenericEqualityComparer%601%5BSystem.String%5D&Count=1&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.String%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.String%5D">EditDocumentRoles</a>

i.e. it's using reflection and working things out completely wrong...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

彩虹直至黑白 2024-09-01 00:29:24

有关如何修复的建议在 MVC3 中对我有用。用法示例:

IDictionary<string, object> routeValues = new Dictionary<string, object>();

routeValues.Add("EmployeeID", 1);

@Html.ActionLink("Employee Details", "EmployeeDetails", "Employee", new RouteValueDictionary(routeValues), null);

The suggestions on how to fix worked for me in MVC3. Example usage:

IDictionary<string, object> routeValues = new Dictionary<string, object>();

routeValues.Add("EmployeeID", 1);

@Html.ActionLink("Employee Details", "EmployeeDetails", "Employee", new RouteValueDictionary(routeValues), null);
超可爱的懒熊 2024-09-01 00:29:24

Graham,

menu.Attributes 是一个 IDictionary并且该方法需要 IDictionaryobject>正确的?将您的键和值复制到另一个字典中,其中值的类型为对象。

Graham,

menu.Attributes is an IDictionary<string, string> and the method expects IDictionary<string, object> right? Copy your keys and values into another dictionary where the values are of type object.

深海夜未眠 2024-09-01 00:29:24

System.Web.Routing 中的 RouteValueDictionary,实际上,是的。此类有一个接受对象或 IDictionary 的构造函数。有一个重载需要这个。因此您可以传递一个 RouteValueDictionary 来代替。

编辑:我认为问题出在这部分:

new Dictionary<string,object>()) 

最后;它应该为空。因为,它所做的就是提取字典的公共属性并被错误地使用。如果更改为 null 可以解决问题,请告诉我。

RouteValueDictionary in System.Web.Routing, actually, so yes. This class has a constructor that takes an object, or an IDictionary<string, object>. There is one overload that takes this. So you can pass a RouteValueDictionary instead.

EDIT: I think the problem is with this part:

new Dictionary<string,object>()) 

at the end; it should be null. Because, what it will do is extract the public properties of the dictionary and be used incorrectly. Let me know if changing to null fixes the issue.

七月上 2024-09-01 00:29:24

我正在使用 MVC 2,并且看到多个 Html.ActionLink 方法将 IDictionary 作为 Html 属性的参数。有什么原因你不能使用其中之一吗?如果您确实需要该方法的较短形式并具有一些默认值,那么您可以在 HtmlHelper 上编写适配器扩展方法。

http:// /msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(VS.90).aspx

也许我不明白你的问题。

I am using MVC 2 and I see more than one Html.ActionLink method that takes an IDictionary as a parameter for the Html attributes. Is there some reason you can't use one of those? If you really need a shorter form of that method with some defaults, then you can write an adapter extension method on HtmlHelper.

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(VS.90).aspx

Perhaps I don't understand your question.

捎一片雪花 2024-09-01 00:29:24

太空牛..我丢失了我用来发布此内容的原始 ID,因此无法直接回答您的问题。

这是 ASP.NET MVC 1.0 的情况。

虽然有一个重载似乎采用了字典,但生成的 HTML 不是字典的内容,而是字典本身的反射类型,基本上没有达到我的预期。在 MVC 框架中,我无法让它接受预定义的键/值列表(例如在字典中)并正确生成 HTML href。我必须创建一个匿名对象,这在我的场景中是不可能的。

Space Cow.. I've lost the original Id I used to post this so cannot answer your question directly.

This was with ASP.NET MVC 1.0.

Although there was an overload that appeared to take a dictonary, the HTML generated was not the contents of the dictionary but the reflected type of the dictionary itself, basically not doing what I'd expect. There was no way in the MVC framework that I could get it to accept a predefined key/value list (e.g. in a dictionary) and for it to generate the HTML href properly. I had to create an anonymous object, which was not possible in my scenario.

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