查看我在部分视图中创建的代码:
<% foreach (Customer customerInfo in Model.DataRows) {%><%}%> <%=Html.ActionLink( customerInfo.FullName , ((string)ViewData["ActionNameForSelectedCustomer"]) , JoinParameters(customerInfo.id, (RouteValueDictionary) ViewData["AdditionalSelectionParameters"]) , null)%> <%=customerInfo.LegalGuardianName %> <%=customerInfo.HomePhone %> <%=customerInfo.CellPhone %>
...script runat="server"...
private RouteValueDictionary JoinParameters(int customerId, RouteValueDictionary defaultValues) { RouteValueDictionary routeValueDictionary = new RouteValueDictionary(defaultValues); routeValueDictionary.Add("customerId", customerId); return routeValueDictionary; } ...script...
<% foreach (Customer customerInfo in Model.DataRows) {%>
<%=Html.ActionLink(
customerInfo.FullName
, ((string)ViewData["ActionNameForSelectedCustomer"])
, JoinParameters(customerInfo.id, (RouteValueDictionary) ViewData["AdditionalSelectionParameters"])
, null)%>
<%=customerInfo.LegalGuardianName %>
<%=customerInfo.HomePhone %>
<%=customerInfo.CellPhone %>
<%}%>
这种方式对我来说非常难看,因为我讨厌在View部分中使用内联代码.
我的问题是 - 有什么更好的方法可以混合从动作传递的参数(在ViewData,TempData,其他...中)和构建动作链接时视图中的参数.
可能是我可以用其他方式建立这个链接?
谢谢!
编写扩展方法以将源字典合并到目标中.
public static class Util { public static RouteValueDictionary Extend(this RouteValueDictionary dest, IEnumerable> src) { src.ToList().ForEach(x => { dest[x.Key] = x.Value; }); return dest; } public static RouteValueDictionary Merge(this RouteValueDictionary source, IEnumerable > newData) { return (new RouteValueDictionary(source)).Extend(newData); } }
用法:
Url.Action(actionName, controllerName, destRvd.Extend(srcRvd));
如果你想要一个包含两个内容的第三个字典 - 使用Merge.但这效率较低.如果您需要合并超过2个字典,此方法可以或多或少地有效地链接合并:
Url.Action(actionName, controllerName, destRvd.Extend(srcRvd1).Extend(srcRvd2));
有效地将3个词典合并为一个新词典:
Url.Action(actionName, controllerName, srcRvd1.Merge(srcRvd2).Extend(srcRvd3));
是 - 您可以RouteValueDictionary
使用Union方法将两个(或更多)对象合并在一起.微软在这里有很好的Union方法示例代码,但微软在使用RouteValueDictionary Unions方面并不是很清楚(因此我的帖子的原因).
鉴于以下课程......
public class Student { public int StudentID { get; set; } public string FirstName { get; set; } public string LastNamse { get; set; } public string Email { get; set; } public DateTime DateOfBirth { get; set; } } public class Address { public string StreetLine1 { get; set; } public string StreetLine2 { get; set; } public string Suburb { get; set; } public string State { get; set; } public string PostCode { get; set; } }
......以及一些示例代码......
Student aStudent = new Student(); aStudent.StudentID = 1234; aStudent.FirstName = "Peter"; aStudent.LastNamse = "Wilson"; aStudent.Email = "peterwilson_69@example.com"; aStudent.DateOfBirth = new DateTime(1969, 12, 31); Address homeAddr = new Address(); homeAddr.StreetLine1 = "Unit 99, Floor 10"; homeAddr.StreetLine2 = "99-199 Example Street"; homeAddr.Suburb = "Sydney"; homeAddr.State = "NSW"; homeAddr.PostCode = "2001";
初始化并向对象添加KeyValue
元素RouteValueDictionary
:
RouteValueDictionary routeStudent = new RouteValueDictionary(aStudent); RouteValueDictionary routeAddress = new RouteValueDictionary(homeAddr);
...使用Union运算符合并两个字典对象:
RouteValueDictionary routeCombined = new RouteValueDictionary(routeStudent.Union(routeAddress).ToDictionary(k => k.Key, k => k.Value));
您现在可以将routeCombined变量传递给适当的方法(例如ActionLink).
@Html.ActionLink("Click to pass routeValues to the RouteAPI", "MyActionName", routeCombined);
写出调试窗口......
foreach (KeyValuePairn in routeCombined) System.Diagnostics.Debug.WriteLine(n.Key + ": " + n.Value);
......将产生以下内容:
StudentID: 1234 FirstName: Peter LastNamse: Wilson Email: peterwilson_69@example.com DateOfBirth: 31/12/1969 12:00:00 AM StreetLine1: Unit 99, Floor 10 StreetLine2: 99-199 Example Street Suburb: Sydney State: NSW PostCode: 2001