0

Can anyone tell me why my parameters in the following code are always null when the controller action is called:

<% 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>



<% } %> 

Controller code:

public class SaleController : Controller
{

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

}
Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59
collusionbdbh
  • 701
  • 1
  • 6
  • 18

3 Answers3

2

Action parameters are bound by name, not by position or type. Therefore you should change id to CustomerID in your calls to Html.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>
lc.
  • 113,939
  • 20
  • 158
  • 187
1

Use the following instead. You're specifying parameters (Controller/Action) that aren't necessary.

<%= Html.ActionLink("Edit", "Edit", "Customer", new { id = row.CustomerID })%>|
<%= Html.ActionLink("Sales", "List", "Sale", new { id = row.CustomerID })%>
Alex
  • 34,899
  • 5
  • 77
  • 90
  • For some reason with your code the Edit customer link still sends a null for the parameter and the Sale list link sends null for the parameter but links to the list action in the customer controller rather than the sale controller – collusionbdbh Jul 17 '13 at 16:48
  • just saw your edit, you named the parameter incorrectly — but it looks like someone else got to it before me. oh well, bills before thrills ... – Alex Jul 17 '13 at 17:00
1

You are sending a parameter named id, but your controller actions are looking for a parameter named CustomerID. These need to match.

cadrell0
  • 17,109
  • 5
  • 51
  • 69