2010-04-01 40 views
7

看我的代碼,我在一個局部視圖創建: 有沒有簡單的方法可以把兩個RouteValueDictionary值傳遞參數Html.ActionLink

  <% 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> 
      <%}%> 

在這裏,我要建簡單表中顯示客戶的詳細信息。

正如你可能看到的,在每一行中,我試圖建立一個鏈接,將重定向到另一個動作。

該操作需要customerId和一些附加參數。

此部分視圖正在使用的每個頁面的其他參數都不相同。 因此,我決定讓Action方法將ViewData中的其他參數作爲RouteValueDictionary實例傳遞。

現在,在視圖我有一個問題,我需要將customerId和RouteValueDictionary一起傳遞到Html.ActionLink方法。 這使我想出如何將所有參數組合到一個對象中(對象或新的RouteValueDictionary實例)

由於MVC的方式,我無法在代碼隱藏中創建方法類(MVC中沒有codebihind),它將加入這些參數。

所以,我用醜陋的方式 - 插入內嵌代碼:

...script runat="server"...

private RouteValueDictionary JoinParameters(int customerId, RouteValueDictionary defaultValues) 
    { 
     RouteValueDictionary routeValueDictionary = new RouteValueDictionary(defaultValues); 

     routeValueDictionary.Add("customerId", customerId); 

     return routeValueDictionary;        
    } 
...script... 

這種方式對我來說是非常難看,因爲我討厭在查看部分使用內嵌代碼。

我的問題是 - 有沒有什麼更好的方式,我可以混合從行動傳遞參數(ViewData,TempData,其他...)和建立行動鏈接時從視圖參數。

可能是我可以用其他方式建立此鏈接?

謝謝!

回答

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)%> 
9

編寫擴展方法來合併源字典到目的地。

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)); 

如果你想要,而不是包含兩個內容的第三詞典 - 使用合併。但是效率不高。這種方法使合併的或多或少的有效鏈接,如果你需要合併2級以上的詞典:

Url.Action(actionName, controllerName, destRvd.Extend(srcRvd1).Extend(srcRvd2)); 

高效合併3個詞典進入一個新問題:

Url.Action(actionName, controllerName, srcRvd1.Merge(srcRvd2).Extend(srcRvd3)); 
5

- 你可以使用聯合方法將兩個(或更多)對象合併在一起。 Microsoft has good example code of the Union method here微軟並不十分清楚當談到使用RouteValueDictionary聯盟(因此我的帖子的原因)。

鑑於以下類...

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 = "[email protected]"; 
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); 

...使用聯合運算符合並兩個字典對象:

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: [email protected] 
DateOfBirth: 31/12/1969 12:00:00 AM 
StreetLine1: Unit 99, Floor 10 
StreetLine2: 99-199 Example Street 
Suburb: Sydney 
State: NSW 
PostCode: 2001 
+0

優秀的答案。 +1。歡迎來到SOF並感謝這篇文章。 – JoshYates1980 2015-06-30 16:11:55

相關問題