看我的代碼,我在一個局部視圖創建: 有沒有簡單的方法可以把兩個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,其他...)和建立行動鏈接時從視圖參數。
可能是我可以用其他方式建立此鏈接?
謝謝!
優秀的答案。 +1。歡迎來到SOF並感謝這篇文章。 – JoshYates1980 2015-06-30 16:11:55