ASP.NET MVC - How to call void controller method without leaving the view?

前端 未结 7 2272
北恋
北恋 2020-12-01 11:51

Question background:

I am implementing some basic \'shopping cart\' logic to an MVC app. Currently when I click a link - denoted as \'Add T

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 12:42

    The answer of Syed Muhammad Zeeshan is what you are looking for, however you may return an EmptyResult.

    public ActionResult AddToCart()
    {
        //Logic to add item to the cart.
        return new EmptyResult();
    }
    

    According to this it has no impact on your code ASP.Net MVC Controller Actions that return void But maybe sometime you want to return data and then you could do something like this:

    if (a) 
    {
        return JSon(data);
    }
    else
    { 
         return new EmptyResult();
    }
    

提交回复
热议问题