我有一個字典傳遞給視圖。我希望能夠將該字典的值從這個相同的View傳遞迴Controller動作。無論如何,這可以在不使用表格的情況下完成嗎?這種情況是我需要能夠將這些回傳給一個控制器動作,當用戶從View中點擊一個ActionLink時會調用這個控制器動作,並且根據我的經驗,ActionLink不能用於提交表單的值,我只能使用表單的提交按鈕提交表單的值。除非有一種方法可以使用ActionLink在表單中提交值。將視圖中的數據傳遞給ASP.Net中的控制器MVC
控制器動作通過字典來控制器中的ViewData:
public ActionResult ModifyNewCrossListing(FormCollection form)
{
Dictionary<int, string> prefixlist = new Dictionary<int, String>();
ViewDatap["prefixList"] = prefixlist;
}
查看打印出詞典的內容,ActionLink的調用另一個控制器動作,這就是我想要訪問的詞典:
<%int i = 0;
if ((ViewData["prefixList"] as Dictionary<int, string>).Count >= 1)
{
foreach (var cp in (ViewData["prefixList"] as Dictionary<int, string>))
{
if (cp.Value == (ViewData["prefixList"] as Dictionary<int, string>).Last().Value)
{%>
<%= Html.Encode(cp.Value)%>
<%}
else
{%>
<%= Html.Encode(cp.Value)%> -
<%}
}
} %>
<%=Html.ActionLink("View All Criteria", "ShowAllCriteria", new { prefixList = ViewData["prefixList"] as Dictionary<int, string> })%>)
最後,在我的新控制器操作中,我希望能夠訪問字典
public ActionResult ShowAllCriteria(Dictionary<int, string> prefixList)
{
//Do stuff
}
代碼將有助於更清楚地解釋您的問題。它看起來像RedirectToAction()可能會幫助你的情況。 – 2010-04-22 17:31:03