【问题标题】:ActionLink optional parameter errorActionLink 可选参数错误
【发布时间】:2011-11-02 18:23:54
【问题描述】:

我在视图中使用以下代码,并尝试使用附加参数在末尾添加提款链接,但出现以下错误:

System.ArgumentException:参数字典包含“MaxMe2”中方法“System.Web.Mvc.ActionResult Withdraw(Int32, Int32)”的不可为空类型“System.Int32”的参数“personID”的空条目。控制器.TeamController'。可选参数必须是引用类型、可空类型或声明为可选参数。

<% if(Model.departmentsDisplayCheck) {%>
<table>
    <tr>         
        <th>Name</th>
        <th>Type</th>
        <th>Status</th> 
    </tr>

<% foreach (var dep in Model.departmentsList){ %>

    <tr>
        <td><%: Html.ActionLink(dep.Name, "Details", "Department", new { id=dep.DepartmentID}, null) %></td>
        <td><%: dep.DepartmentType.Type %></td>
        <td><%: dep.DepartmentStatus.Status %></td>
        <td><%: Html.ActionLink("Withdraw", "Withdraw", "Team", new { id = Model.personalInfo.PersonID, dep = dep.DepartmentID}, null)%></td>
    </tr>
<% } %>

我试图调用的控制器方法是这样的:

public ActionResult Withdraw(int personID, int departmentID)
    {
        .....
    }

我怎样才能做到这一点?提前致谢!

【问题讨论】:

    标签: c# asp.net-mvc-2


    【解决方案1】:

    您的参数名称与操作链接发送的参数名称不匹配。试试这样:

    public ActionResult Withdraw(int id, int dep)
    

    或更新您的 ActionLink 参数名称以匹配操作的名称:

    <%= Html.ActionLink(
        "Withdraw", 
        "Withdraw", 
        "Team", 
        new { 
            personID = Model.personalInfo.PersonID, 
            departmentID = dep.DepartmentID
        },
        null
    ) %>
    

    然后:

    public ActionResult Withdraw(int personID, int departmentID)
    

    会工作的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多