ASP.NET MVC-如何在不离开视图的情况下调用void控制器方法? [英] ASP.NET MVC - How to call void controller method without leaving the view?

查看:132
本文介绍了ASP.NET MVC-如何在不离开视图的情况下调用void控制器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题背景:

Question background:

我正在为MVC应用程序实现一些基本的购物车"逻辑.当前,当我单击下面的屏幕快照中表示为添加到购物车"的链接时,这将调用"ProductController"中的"AddToCart"方法,如下所示:

I am implementing some basic 'shopping cart' logic to an MVC app. Currently when I click a link - denoted as 'Add To Cart' on the screen shot below this calls to an 'AddToCart' method in the 'ProductController' as shown:

Product.cshtml代码:

Product.cshtml code:

@Html.ActionLink("Add To Cart", "AddToCart")

ProductController中的

'AddToCart'方法:

'AddToCart' method in the ProductController:

public void AddToCart()
{
   //Logic to add item to the cart.
}

问题:

The issue:

不是这样的问题,但是当前当我单击ProductDetail.cshtml视图上的ActionLink上的添加到购物车"按钮时,该页面在ProductController上调用了"AddToCart"方法,并在页面上显示了空白视图-如下所示. 我希望视图保留在"ProductDetail.cshtml"上,仅调用"AddToCart"方法,该怎么做?

Not an issue as such but currently when I click the 'Add To Cart' button on the ActionLink on the ProductDetail.cshtml view the page calls the 'AddToCart' method on the ProductController and gives a blank view on the page - as shown below. I want the view to stay on 'ProductDetail.cshtml' and just call the 'AddToCart' method, how do I do this?

推荐答案

基本上,@Html.ActionLink()<a></a>标记使用get请求来定位页面.因此,每当您单击它时,都需要在 ProductController 中请求 AddToCart 操作方法,如果该操作方法返回null或void,那么您会看到空白或空白的页面(因为或默认情况下@ Html.ActionLink()会获得请求.)

Basically @Html.ActionLink() or <a></a> tag uses get request to locate the page. Hence whenever you clicked it, you request to your AddToCart action method in ProductController and if that action method returns null or void so a blank or empty page is shown as you experienced (because or @Html.ActionLink() get request by Default).

因此,如果要将值添加到购物车,请使用ajax调用 AddToCart 方法,即:

So if you want to add your value to cart then call AddToCart method using ajax i.e:

HTML:

@Html.ActionLink("Add To Cart", "AddToCart", null, new { id="myLink"})

jQuery或Javascript:

$("#myLink").click(function(e){

    e.preventDefault();
    $.ajax({

        url:$(this).attr("href"), // comma here instead of semicolon   
        success: function(){
        alert("Value Added");  // or any other indication if you want to show
        }

    });

});

ProductController中的

'AddToCart'方法:

'AddToCart' method in the ProductController:

public void AddToCart()
{
   //Logic to add item to the cart.
}

现在这一次,当调用转到 AddToCart 方法时,它将使用ajax进行调用,因此整个页面将不会重定向或更改,而是一个异步调用,该异步调用将在您的中执行AddToCart动作方法> ProductController ,当前页面将保持不变.因此,该产品也将添加到购物车,并且页面不会更改为空白.

Now this time when the call goes to AddToCart method it goes by using ajax hence the whole page will not redirect or change, but its an asynchronous call which execute the AddToCart action method in your ProductController and the current page will remains same. Hence the product will also added to cart and page will not change to blank.

希望这会有所帮助.

这篇关于ASP.NET MVC-如何在不离开视图的情况下调用void控制器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆