查看我在部分视图中创建的代码:
            <% foreach (Customer customerInfo in Model.DataRows) {%>
            <tr>
                <td>
                    <%=Html.ActionLink(
                            customerInfo.FullName
                            , ((string)ViewData["ActionNameForSelectedCustomer"])
                            , JoinParameters(customerInfo.id, (RouteValueDictionary) ViewData["AdditionalSelectionParameters"])
                            , null)%>
                </td>
                <td>
                    <%=customerInfo.LegalGuardianName %>
                </td>
                <td>
                    <%=customerInfo.HomePhone %>
                </td>
                <td>
                    <%=customerInfo.CellPhone %>
                </td>
            </tr>
            <%}%>
...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) {%>
            <tr>
                <td>
                    <%=Html.ActionLink(
                            customerInfo.FullName
                            , ((string)ViewData["ActionNameForSelectedCustomer"])
                            , JoinParameters(customerInfo.id, (RouteValueDictionary) ViewData["AdditionalSelectionParameters"])
                            , null)%>
                </td>
                <td>
                    <%=customerInfo.LegalGuardianName %>
                </td>
                <td>
                    <%=customerInfo.HomePhone %>
                </td>
                <td>
                    <%=customerInfo.CellPhone %>
                </td>
            </tr>
            <%}%>
Run Code Online (Sandbox Code Playgroud)
这种方式对我来说非常难看,因为我讨厌在View部分中使用内联代码.
我的问题是 - 有什么更好的方法可以混合从动作传递的参数(在ViewData,TempData,其他...中)和构建动作链接时视图中的参数.
可能是我可以用其他方式建立这个链接?
谢谢!
Zar*_*dan 10
编写扩展方法以将源字典合并到目标中.
public static class Util
{
    public static RouteValueDictionary Extend(this RouteValueDictionary dest, IEnumerable<KeyValuePair<string, object>> src)
    {
        src.ToList().ForEach(x => { dest[x.Key] = x.Value; });
        return dest;
    }
    public static RouteValueDictionary Merge(this RouteValueDictionary source, IEnumerable<KeyValuePair<string, object>> 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));
小智 6
是 - 您可以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<string, object>元素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 (KeyValuePair<string, object> n 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
小智 2
也许实现此目的的一个简单方法是创建一个 UrlHelper 扩展,它执行与 JoinParameters 方法类似的操作。像下面的方法更具可重用性:
public static RouteValueDictionary AppendRouteValue(this UrlHelper helper, RouteValueDictionary routeValues, string key, object value)
{
    RouteValueDictionary routeValueDictionary = new RouteValueDictionary(routeValues);
    routeValueDictionary.Add(key, value);
    return routeValueDictionary;
}
现在你的行动就像这样:
<%=Html.ActionLink(
    customerInfo.FullName
    , ((string)ViewData["ActionNameForSelectedCustomer"])
    , Url.AppendRouteValue((RouteValueDictionary) ViewData["AdditionalSelectionParameters"], "customerId", customerInfo.id)
    , null)%>
| 归档时间: | 
 | 
| 查看次数: | 7064 次 | 
| 最近记录: |