ActionLink可选参数错误

时间:2011-11-02 18:23:54

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

我在视图中使用以下代码,并且我尝试使用附加参数在末尾添加withdraw链接,但是我收到以下错误:

System.ArgumentException:参数字典包含'MaxMe2中方法'System.Web.Mvc.ActionResult Withdraw(Int32,Int32)'的非可空类型'System.Int32'的参数'personID'的空条目。 Controllers.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)
    {
        .....
    }

我该如何使这项工作?提前谢谢!

1 个答案:

答案 0 :(得分:6)

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

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)

将会起作用。