出于某种原因,我无法获得被击中的动作。我正在使用RedirectToAction
将流从一个动作传递到另一个动作,但它不会进入该功能。
这是第一个动作:
Public Function AddToCart(productId As Integer, returnUrl As String) As RedirectToRouteResult
Dim product As Product = Me.Repository.Products _
.FirstOrDefault(Function(p) p.ProductId = productId)
If Not IsNothing(product) Then
Me.Cart.AddItem(product, 1)
End If
Return RedirectToAction("Index", New With {.returnUrl = returnUrl})
End Function
该动作被击中(由断点验证)。通过Return RedirectToAction("List", New With {.returnUrl = returnUrl})
调用的后续操作(索引)永远不会被命中(也可以通过断点验证。
这是索引动作功能:
Public Function Index(returnUrl As String) As ViewResult
Dim cartIndexViewModel As New CartIndexViewModel() With { _
.Cart = Me.Cart, _
.ReturnUrl = returnUrl _
}
Return View(cartIndexViewModel)
End Function
我不确定它是否相关,但值得注意的是,我为索引操作创建了一个视图,但后来删除并重新创建了它。
作为一个完整性检查,我创建了另一个名为列表的动作,它与索引动作相同,并且此动作确实被击中(再次,由断点验证)
这是我的控制器的全部代码:
Imports SportsStore.Domain
Namespace SportsStore.WebUI
Public Class CartController
Inherits System.Web.Mvc.Controller
#Region "Properties"
Public Property Cart As Cart
Get
If IsNothing(Session("Cart")) Then
Session("Cart") = New Cart()
End If
Return CType(Session("Cart"), Cart)
End Get
Set(value As Cart)
Session("Cart") = value
End Set
End Property
Private _Repository As IProductRepository
Private Property Repository() As IProductRepository
Get
Return _Repository
End Get
Set(ByVal value As IProductRepository)
_Repository = value
End Set
End Property
#End Region 'Properties
#Region "Constructors"
Public Sub New(repository As IProductRepository)
Me.Repository = repository
End Sub
#End Region 'Constructors
#Region "Actions"
'
' GET: /Cart
Public Function AddToCart(productId As Integer, returnUrl As String) As RedirectToRouteResult
Dim product As Product = Me.Repository.Products _
.FirstOrDefault(Function(p) p.ProductId = productId)
If Not IsNothing(product) Then
Me.Cart.AddItem(product, 1)
End If
'DOES NOT WORK
'Return RedirectToAction("List", New With {.returnUrl = returnUrl})
'WORKS
Return RedirectToAction("Index", New With {.returnUrl = returnUrl})
End Function
Public Function RemoveFromCart(productId As Integer, returnUrl As String) As RedirectToRouteResult
Dim product As Product = Me.Repository.Products _
.FirstOrDefault(Function(p) p.ProductId = productId)
If Not IsNothing(product) Then
Me.Cart.RemoveLine(product)
End If
Return RedirectToAction("Index", New With {.returnUrl = returnUrl})
End Function
Public Function List(returnUrl As String) As ViewResult
Dim cartIndexViewModel As New CartIndexViewModel() With { _
.Cart = Me.Cart, _
.ReturnUrl = returnUrl _
}
Return View(cartIndexViewModel)
End Function
Public Function Index(returnUrl As String) As ViewResult
Dim cartIndexViewModel As New CartIndexViewModel() With { _
.Cart = Me.Cart, _
.ReturnUrl = returnUrl _
}
Return View(cartIndexViewModel)
End Function
#End Region 'Actions
End Class
End Namespace
这是项目的结构:
我错过了什么?
答案 0 :(得分:0)
根据您的代码,您似乎从AddToCart()重定向到Index操作而不是List操作。
Return RedirectToAction("Index", New With {.returnUrl = returnUrl})