ActionLink可选参数错误

Tsa*_*arl 1 c# asp.net-mvc-2

我在视图中使用以下代码,我试图在最后使用附加参数添加撤销链接但我收到以下错误:

System.ArgumentException:参数字典包含'MaxMe2.Controllers.TeamController中方法'System.Web.Mvc.ActionResult Withdraw(Int32,Int32)'的非可空类型'System.Int32'参数'personID'的空条目".可选参数必须是引用类型,可空类型,或者声明为可选参数.

<% 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>
<% } %>
Run Code Online (Sandbox Code Playgroud)

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

public ActionResult Withdraw(int personID, int departmentID)
    {
        .....
    }
Run Code Online (Sandbox Code Playgroud)

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

Dar*_*rov 6

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

public ActionResult Withdraw(int id, int dep)
Run Code Online (Sandbox Code Playgroud)

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

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

然后:

public ActionResult Withdraw(int personID, int departmentID)
Run Code Online (Sandbox Code Playgroud)

将要工作.