2009-10-07 45 views
1

我有一個名爲TicketsController.vb控制器,具有動作結果:我可以比在控制器的一個「細節」的ActionResult更多?

' 
' GET: /Tickets/Details/5 
Public Function Details(ByVal id As Integer) As ActionResult 
    ViewData("OpenTixCount") = ticketRepository.countOpenTickets.Count() 
    ViewData("UrgentTixCount") = ticketRepository.countUrgentTickets.Count() 
    ViewData("HighTixCount") = ticketRepository.countHighTickets.Count() 
    ViewData("NormalTixCount") = ticketRepository.countNormalTickets.Count() 
    ViewData("LowTixCount") = ticketRepository.countLowTickets.Count() 

    Dim ticket As hdCall = ticketRepository.GetTicket(id) 

    If ticket Is Nothing Then 
     Return View("NotFound") 
    Else 
     ViewData("MyTicketID") = ticket.CallID 
     ViewData("UserThatLogged") = ticket.hdUser.RealName 
     ViewData("CustomerName") = ticket.hdCustomer.CustomerName 
     ViewData("TimeLogged") = ticket.loggedOn.ToLongDateString & " " & ticket.loggedOn.ToLongTimeString 
     ViewData("CustomerID") = ticket.CustomerID 
     ViewData("CustomerEmail") = ticket.hdCustomer.Email 

     Dim custID As Integer = ticket.CustomerID 
     Dim TicketsForCustomer = ticketRepository.GetTicketsForThisCustomer(custID).Count() 

     ViewData("TicketsForCustomerCount") = TicketsForCustomer 
     Dim dataContext As New CustomerServicesDataContext 
     ViewData("TicketStatus") = New SelectList(dataContext.hdStatus, "StatusID", "Status", ticket.StatusID) 
     ViewData("TicketType") = New SelectList(dataContext.hdCategories, "CategoryID", "Title", ticket.CategoryID) 
     ViewData("TicketPriority") = New SelectList(dataContext.hdPriorities, "PriorityID", "Priority", ticket.PriorityID) 
     ViewData("CompanyType") = New SelectList(dataContext.hdCompanies, "CompanyID", "Company", ticket.CompanyID) 
     ViewData("CallDetails") = ticket.CallDetails 

    End If 

    Return View(ticket) 
End Function 

我還添加了另一個叫做客戶,其目的是通過搶客戶ID門票的列表。我使用的是做到這一點的代碼是:

' 
'GET: /Tickets/Customer/1 
Public Function Customer(ByVal custID As Integer) As ActionResult 
    ViewData("OpenTixCount") = ticketRepository.countOpenTickets.Count() 
    ViewData("UrgentTixCount") = ticketRepository.countUrgentTickets.Count() 
    ViewData("HighTixCount") = ticketRepository.countHighTickets.Count() 
    ViewData("NormalTixCount") = ticketRepository.countNormalTickets.Count() 
    ViewData("LowTixCount") = ticketRepository.countLowTickets.Count() 

    Dim cust As hdCustomer = customerRepository.GetCustomerDetails(custID) 

    Dim tickets = ticketRepository.FindAllTicketsForThisCustomer(custID) 

    ViewData("CustomerName") = cust.CustomerName 
    ViewData("CustomerEmail") = cust.Email 
    Dim TicketsForCustomer = ticketRepository.GetTicketsForThisCustomer(custID).Count() 

    ViewData("TicketsForCustomerCount") = TicketsForCustomer 

    Return View(tickets) 
End Function 

然而,當我訪問/票/客戶/ 1例如網頁,我收到錯誤:

The parameters dictionary contains a null entry for parameter 'custID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Customer(Int32)' in 'CustomerServicesHelpdesk.TicketsController'. To make a parameter optional its type should be either a reference type or a Nullable type. Parameter name: parameters

我「M不太肯定我要去哪裏錯了這裏,但不可能是因爲我有2個ActionResults尋找類似的路線?

感謝您提前提供任何幫助。

回答

2

此問題araises因爲在ASP.NET MVC的默認航線預計參數被命名id,但你的客戶方法接受一個名爲custID參數。

速戰速決就是重命名custIDid

一種替代的解決方案是在Global.asax中添加自定義路由。像這樣的東西

routes.MapRoute(
    "Tickets",             // Route name 
    "Tickets/Customers/{custID}",        // URL with parameters 
    new { controller = "Home", action = "Index", custID = "" } // Parameter defaults 
    ); 
1

請問您可以發佈您的路線定義嗎?如果您使用的是默認路由的定義,試圖CUSTID在方法定義重命名爲ID,使之成爲:

Public Function Customer(ByVal id As Integer) As ActionResult 
相關問題