我正在嘗試使用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);
}