0

谁能告诉我为什么在调用控制器操作时我在以下代码中的参数始终为空:

<% foreach (var row in Model) { %>
     <tr>
        <td><%=Html.ActionLink("Edit", "Edit", "Customer", new { controller = "Customer", action = "Edit", id = row.CustomerID })%>|
            <%= Html.ActionLink("Sales", "List", "Sale", new { controller = "Sale", action = "List", id = row.CustomerID }, null)%></td>
        <td><%= Html.Encode(row.CustomerID)%> </td>
        <td><%= Html.Encode(row.FirstName)%> </td>
        <td><%= Html.Encode(row.LastName)%> </td>
        <td><%= Html.Encode(String.Format("{0:g}", row.DateOfBirth))%></td>
        <td><%= Html.Encode(row.Address)%> </td>
        <td><%= Html.Encode(row.Phone)%> </td>
    </tr>



<% } %> 

控制器代码:

public class SaleController : Controller
{

    public ActionResult List(int CustomerID)
    {
        SaleListModel SaleList = SaleServices.GetList(CustomerID);
        return View(SaleList);
    }

}
4

3 回答 3

2

动作参数受名称约束,而不是位置或类型。因此,您应该在调用中更改id为:CustomerIDHtml.ActionLink

    <td><%=Html.ActionLink("Edit", "Edit", "Customer", new { controller = "Customer", action = "Edit", CustomerID = row.CustomerID })%>|
        <%= Html.ActionLink("Sales", "List", "Sale", new { controller = "Sale", action = "List", CustomerID = row.CustomerID }, null)%></td>
于 2013-07-17T16:55:48.460 回答
1

请改用以下内容。您正在指定不必要的参数(控制器/操作)。

<%= Html.ActionLink("Edit", "Edit", "Customer", new { id = row.CustomerID })%>|
<%= Html.ActionLink("Sales", "List", "Sale", new { id = row.CustomerID })%>
于 2013-07-17T16:43:02.613 回答
1

您正在发送一个名为 的参数id,但您的控制器操作正在寻找一个名为 的参数CustomerID。这些需要匹配。

于 2013-07-17T16:55:26.977 回答