For some reason, I cannot get an action to be hit. I am using RedirectToAction to pass the flow from one action to another, but it does not enter that function.
Here is the first action:
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
That action gets hit (verified by a breakpoint). The subsequent action (Index) called via Return RedirectToAction("List", New With {.returnUrl = returnUrl}) never gets hit (also verified by a breakpoint.
This is the Index action 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
I am not sure it is relevant, but it might be worth to note that I created a View for the Index action, but later deleted and recreated it.
As a sanity check I then created another Action named List that is identical to the Index action, and this action does get hit (again, verified by a breakpoint)
This is the code of my controller in its entirety:
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
And here is the structure of the project:

What am I missing?
Based on your code it appears you are redirecting to the Index action rather than the List action, from AddToCart().