我可以有多个“详细信息”吗?控制器中的 ActionResult?
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出现此问题的原因是 ASP.NET MVC 中的默认路由期望参数名为
id
,但您的 Customer 方法采用名为custID
的参数。快速解决方法是将
custID
重命名为id
。另一种解决方案是在 Global.asax 中添加自定义路由。像这样的东西
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 namedcustID
.A quick fix would be to rename
custID
toid
.An alternative solution is to add a custom route in Global.asax. Something like this
您能发布您的路线定义吗?如果您使用默认路由定义,请尝试在方法定义中将 custID 重命名为 id,使其变为:
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: