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

发布于 2024-08-07 03:44:46 字数 3263 浏览 4 评论 0原文

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

'
' 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”的空条目 不可为空类型“System.Int32” 方法'System.Web.Mvc.ActionResult 客户(Int32)' in 'CustomerServicesHelpdesk.TicketsController'。 使参数可选其类型 应该是引用类型或 可空类型。参数名称: 参数

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耶耶耶 2024-08-14 03:44:46

出现此问题的原因是 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
    );

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
    );
汹涌人海 2024-08-14 03:44:46

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

Public Function Customer(ByVal id As Integer) As ActionResult

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文