ASP MVC3 ActionLink不呈现变量

时间:2013-04-04 23:09:36

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 actionlink

我正在尝试使用ASP MVC3操作链接导航到另一个视图(同一个控制器)。该视图附加到使用复合键作为其主键的模型。下面是在视图上写的操作链接

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

2 个答案:

答案 0 :(得分: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,string,string,string,object)但是(HtmlHelper,string,string,string,object,object)其中第二个最后一个对象是路由值,最后一个是html属性。

答案 1 :(得分:0)

根据您提供的参数,调用错误的ActionLink。

问题在于它是“尝试序列化字符串对象”

以下是链接中“'长度'参数”的规范答案“问题: Why does Html.ActionLink render "?Length=4"