Gra*_*ham 6 asp.net asp.net-mvc actionlink routevalues
所有,
掌握ASP.NET MVC.到目前为止,这么好,但这个有点疯了.
我有一个视图模型,其中包含超链接的属性字典,如下所示:
menu =模型变量
Html.ActionLink(Html.Encode(menu.Name), Html.Encode(menu.Action), Html.Encode(menu.Controller), menu.Attributes, null)
Run Code Online (Sandbox Code Playgroud)
问题是"menu.Attributes"的位置需要表单中的对象:
new { Name = "Fred", Age=24 }
Run Code Online (Sandbox Code Playgroud)
据我所知,这个匿名对象实际上是通过反射转换为字典,但是你不能在第一时间将字典传递给它!
为链接生成的Html只显示字典类型.
我到底怎么绕这个?重点是它的一般和控制器可以设置菜单.以前的属性....
基于下面的帖子,我尝试了以下内容:
Html.ActionLink(Html.Encode(menu.Name), Html.Encode(menu.Action), Html.Encode(menu.Controller), new RouteValueDictionary(menu.Attributes), new Dictionary<string,object>())
Run Code Online (Sandbox Code Playgroud)
但这仍然不起作用(我猜代码内部调用带对象的泛型方法?).以上(以及我将字典传递给第4个参数的原始解决方案产生类似于此的HTML:
<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>
Run Code Online (Sandbox Code Playgroud)
即它使用反射和工作完全错误...
小智 5
关于如何修复的建议在MVC3中为我工作.用法示例:
IDictionary<string, object> routeValues = new Dictionary<string, object>();
routeValues.Add("EmployeeID", 1);
@Html.ActionLink("Employee Details", "EmployeeDetails", "Employee", new RouteValueDictionary(routeValues), null);
Run Code Online (Sandbox Code Playgroud)
RouteValueDictionary实际上System.Web.Routing,所以是的。这个类有一个构造函数,它接受一个对象,或者一个IDictionary<string, object>. 有一个重载需要这个。所以你可以通过 aRouteValueDictionary来代替。
编辑:我认为问题出在这部分:
new Dictionary<string,object>())
Run Code Online (Sandbox Code Playgroud)
在最后; 它应该为空。因为,它所做的就是提取字典的公共属性并被错误地使用。如果更改为 null 可以解决问题,请告诉我。
| 归档时间: |
|
| 查看次数: |
12079 次 |
| 最近记录: |