Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 为什么这个动作没有被击中?_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 为什么这个动作没有被击中?

Asp.net mvc 3 为什么这个动作没有被击中?,asp.net-mvc-3,Asp.net Mvc 3,由于某种原因,我无法让动作被击中。我正在使用RedirectToAction将流从一个动作传递到另一个动作,但它没有进入该函数 以下是第一个行动: Public Function AddToCart(productId As Integer, returnUrl As String) As RedirectToRouteResult Dim product As Product = Me.Repository.Products _ .FirstOrDefault(Func

由于某种原因,我无法让动作被击中。我正在使用
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”,使用{.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
我不确定它是否相关,但值得注意的是,我为索引操作创建了一个视图,但后来删除并重新创建了它

作为一项健全性检查,我随后创建了另一个名为List的操作,该操作与Index操作相同,并且该操作确实被命中(再次通过断点验证)

这是我的控制器的全部代码:

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
以下是该项目的结构:


我缺少什么?

根据您的代码,您似乎正在从AddToCart()重定向到索引操作而不是列表操作


我希望代码重定向到索引,但它没有到达那里。您看到的是我对我创建的另一个名为List的方法的健全性检查。您可能在这个问题中有输入错误…您的代码块显示
Return RedirectToAction(“Index”)。
但您的内联代码显示
Return RedirectToAction(“List”)…
@Scottipey:在回答中修复了现在你已经更改了问题,我看不出有任何问题。你重定向到“列表”,然后“列表”被点击,“索引”没有被点击。有什么问题吗?@Scottipey:抱歉。我已经清理了动作调用。知道为什么索引没有被点击吗?
Return RedirectToAction("Index", New With {.returnUrl = returnUrl})