views:

174

answers:

2

I have a Controller called TicketsController.vb, with an action result of:

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

I've also added another called Customers, which is intended to grab a list of tickets by Customer ID. The code I'm using to do this is:

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

However, when I access the page of /Tickets/Customers/1 for example, I receive the error:

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

I'm not quite sure where I'm going wrong here, but could it be because I have 2 ActionResults looking for similar routes?

Thanks for any help in advance.

+2  A: 

This issue araises because the default route in ASP.NET MVC expects the parameter to be named id, but your Customer method takes a parameter named custID.

A quick fix would be to rename custID to id.

An alternative solution is to add a custom route in Global.asax. Something like this

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

Could you please post your routes definition? If you are using the default routing definitions, try to rename custID to id in the method definition, so that it becomes:

Public Function Customer(ByVal id As Integer) As ActionResult
Palantir