我可以在控制器中有多个“详细信息”ActionResult吗?

时间:2009-10-07 10:52:09

标签: asp.net-mvc model-view-controller asp.net-mvc-routing

我有一个名为TicketsController.vb的Controller,其操作结果为:

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

我还添加了另一个名为Customers的用户,用于按客户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

但是,当我访问 / Tickets / Customers / 1 的页面时,我收到错误:

  

参数字典包含一个   参数'custID'的null条目   非可空类型'System.Int32'   方法'System.Web.Mvc.ActionResult   客户(Int32)'   'CustomerServicesHelpdesk.TicketsController'。   使参数可选为其类型   应该是引用类型或   可空类型。参数名称:   参数

我不太确定我在哪里出错了,但可能是因为我有2个ActionResults寻找类似的路线?

感谢您提前提供任何帮助。

2 个答案:

答案 0 :(得分:2)

此问题因为ASP.NET MVC中的默认路由要求将参数命名为id,但您的Customer方法采用名为custID的参数。

快速解决方法是将custID重命名为id

另一种解决方案是在Global.asax中添加自定义路由。像这样的东西

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

答案 1 :(得分:1)

您能否发布您的路线定义?如果您使用的是默认路由定义,请尝试在方法定义中将custID重命名为id,以便它变为:

Public Function Customer(ByVal id As Integer) As ActionResult