2013-07-17 77 views
0

誰能告訴我爲什麼我在下面的代碼參數總是空當控制器動作被稱爲:MVC ActionLink的參數從未發送

<% 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); 
    } 

} 
+0

其作用???? – xandercoded

+0

這兩個操作鏈接都將參數發送爲空 – collusionbdbh

回答

1

您發送一個名爲id參數,但你的控制器動作都在尋找一個叫CustomerID參數。這些需要匹配。

+0

謝謝,那就是訣竅。自從你第一次以來,你的回答就是正確的。 – collusionbdbh

1

改用以下。您正在指定不需要的參數(控制器/操作)。

<%= Html.ActionLink("Edit", "Edit", "Customer", new { id = row.CustomerID })%>| 
<%= Html.ActionLink("Sales", "List", "Sale", new { id = row.CustomerID })%> 
+0

出於某種原因,您的代碼中的Edit customer鏈接仍然爲參數發送null,而Sale list鏈接爲參數發送null,但鏈接到客戶控制器中的list action比銷售控制器 – collusionbdbh

+0

添加適用的控制器代碼以獲得更完整的答案... – xandercoded

+0

已添加控制器代碼 – collusionbdbh

2

動作參數綁定名稱,而不是按位置或類型。因此,你應該在你的電話改變idCustomerIDHtml.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>