2013-04-04 69 views
1

我正在嘗試使用ASP MVC3操作鏈接導航到另一個視圖(同一控制器)。該視圖附加到使用複合鍵作爲其主鍵的模型。下面是因爲它是寫在視圖ASP MVC3 ActionLink無法呈現變量

@Html.ActionLink("Edit Agent", "AgentEdit", "BankListMasterController", 
               new { @agentId = int.Parse(item.AgentId), @id = item.ID}) 

但是操作鏈接時,這個被渲染它呈現爲以下

http://localhost:2574/BankListMaster/AgentEdit?Length=24 

這顯然會引發錯誤:

The parameters dictionary contains a null entry for parameter 'agentId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult AgentEdit(Int32, Int32)' in 'Monet.Controllers.BankListMasterController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. 
Parameter name: parameters 

這裏控制器方法的好措施:

public ViewResult AgentEdit(int agentId, int id) 
    { 
     string compare = agentId.ToString(); 

     BankListAgentId agent = (from c in db.BankListAgentId 
           where c.ID == id && 
             c.AgentId.Equals(compare) 
           select c).Single(); 

     return View("AgentEdit", agent); 
    } 

回答

3
@Html.ActionLink("Edit Agent", "AgentEdit", "BankListMasterController", 
               new { agentId = int.Parse(item.AgentId), id = item.ID}, null) 

這應該做的伎倆

和理由是:根據http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.108).aspx

你不會找到那裏(的HtmlHelper,字符串,字符串,字符串對象)的方法但是有(的HtmlHelper,串,字符串,字符串,對象,對象)第二個最後一個對象是路由值,最後一個是html屬性。

0

根據您提供的參數,正在調用錯誤的ActionLink。

的問題是,它是「試圖序列字符串對象」

這裏是典型答案「的鏈接‘長度’參數」問題: Why does Html.ActionLink render "?Length=4"