Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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
C# 将数据从控制器传递到视图没有响应。代码中有什么错误吗?_C#_Linq_Asp.net Mvc 4 - Fatal编程技术网

C# 将数据从控制器传递到视图没有响应。代码中有什么错误吗?

C# 将数据从控制器传递到视图没有响应。代码中有什么错误吗?,c#,linq,asp.net-mvc-4,C#,Linq,Asp.net Mvc 4,除了从购物车中删除选项外,我正在编写的网站运行良好。我没有在代码中得到任何错误,但是当我单击从购物车中删除的链接时,代码没有任何作用。这一页似乎刚刚刷新。我正在用C#.NETMVC4对系统进行编码。有人能帮我吗 这是ShoppingCartController.cs的一部分 [HttpPost] public ActionResult RemoveFromCart(int id) { // Remove the item from the cart var cart = Shop

除了从购物车中删除选项外,我正在编写的网站运行良好。我没有在代码中得到任何错误,但是当我单击从购物车中删除的链接时,代码没有任何作用。这一页似乎刚刚刷新。我正在用C#.NETMVC4对系统进行编码。有人能帮我吗

这是ShoppingCartController.cs的一部分

[HttpPost]
public ActionResult RemoveFromCart(int id)
{
    // Remove the item from the cart
    var cart = ShoppingCart.GetCart(this.HttpContext);

    // Get the name of the album to display confirmation
    string albumName = storeDB.Carts
            .Single(item => item.RecordId == id).Album.Title;

    // Remove from cart
    int itemCount = cart.RemoveFromCart(id);

    // Display the confirmation message
    var results = new ShoppingCartRemoveViewModel
    {
        Message = Server.HtmlEncode(albumName) +
                " has been removed from your shopping cart.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount(),
                ItemCount = itemCount,
                DeleteId = id
    };

    return Json(results);
}
这是ShoppingCart模型的一部分

public int RemoveFromCart(int id)
{
    // Get the cart
    var cartItem = storeDB.Carts.Single(
    cart => cart.CartId == ShoppingCartId
        && cart.RecordId == id);

    int itemCount = 0;

    if (cartItem != null)
    {
        if (cartItem.Count > 1)
        {
            cartItem.Count--;
            itemCount = cartItem.Count;
        }
        else
        {
            storeDB.Carts.Remove(cartItem);
        }

        // Save changes
        storeDB.SaveChanges();
    }

    return itemCount;
}
最后,这是ShoppingCart/索引视图

@foreach (var item in Model.CartItems)
{
    <tr id="row-@item.RecordId">
        <td>
            @Html.ActionLink(item.Album.Title, "Details", "Shop", new { id = item.AlbumId }, null)
        </td>
        <td>
            @item.Album.Price
        </td>
        <td id="item-count-@item.RecordId">
            @item.Count
        </td>
        <td>
            <a href="#" class="RemoveFromCart" data-id="(@item.RecordId == id).Album.Title">Remove from cart</a>
        </td>
    </tr>
}
@foreach(Model.CartItems中的变量项)
{
@Html.ActionLink(item.Album.Title,“Details”,“Shop”,新建{id=item.AlbumId},null)
@物品、相册、价格
@项目。计数
}

除非您在某个地方有一些javascript正在处理“从购物车中删除”链接的
onclick
,或者在其中添加
href
,否则您当前的
href
“#”
似乎会像您描述的那样“刷新页面”。考虑将锚点标记更改为<代码> Action Link < /代码>,如您的细节链接,但指向您的<代码> ReleVoCuto Action方法。

< P>您需要使用<代码> HTML。Action Link < /C> >而不是<代码> A<代码>标签,就像您为商店/细节动作所做的那样。示例(假设您的
RemoveFromCart
操作位于
ShopController
中,并且您希望删除id=item.AlbumId的项目):


这样将执行完整页面刷新,您需要从操作返回视图。要仅进行部分刷新,您需要使用Ajax.ActionLinkhelper。

您尝试过调试代码吗?是的,我尝试过,但它没有链接到ShoppingCartController中的RemoveFromCart。我已经按照您的建议完成并添加了以下代码@ActionLink(“从购物车中删除”、“CartSummary”、“ShoppingCart”、new{id=item.AlbumId}、new{@class=“RemoveFromCart”})但是,当我单击从购物车中删除时,出现了一个错误,表示“操作‘CartSummary’只能通过子请求访问”。您的CartSummary操作是否具有[ChildActionOnly]属性?另外,请注意Html.ActionLink只发送get请求。这是CartSummary操作的代码:@Html.ActionLink(“Cart”(+ViewData[“CartCount”]+”)、“Index”、“ShoppingCart”、new{id=“Cart status”})我不确定您的意思,请参阅抱歉,我给了您CartSummary视图的代码。是,购物车摘要确实具有[ChildActionOnly]属性。
@Html.ActionLink(
     "Remove from cart", 
     "RemoveFromCart", 
     "Shop", 
     new { id = item.AlbumId }, 
     new { @class = "RemoveFromCart"})