0

I am making an ajax call to a method in my controller. The method returns a Partial View ActionResult back to the success method in the ajax call. I want to pass an additional message or variable to that success message so I can check for something. Here is my current javascript ajax call:

$.ajax({
                url: '@Url.Action("_AddToCart", "Cart")',
                data: {
                    id: id,
                    qty: quantity
                },
                success: function (response) {
                    var cartLink = $(".add").attr("data-url");
                    $("#summary").html(response);

I am setting the HTML of a div to the entire response. How can I pass an additional message or variable from the controller back to the success function?

dmikester1
  • 1,374
  • 11
  • 55
  • 113
  • Would it work to use ViewData? Can I read that in my Javascript? – dmikester1 Sep 08 '14 at 18:08
  • Try adding data in ViewData and access it in javascript. Check this http://stackoverflow.com/questions/2374707/how-to-access-viewdata-in-javascript – malkam Sep 09 '14 at 06:05

1 Answers1

1

There are a few ways to do this.

  • You could include some JavaScript in the partial view that runs when added to the DOM. That JavaScript could be rendered in your partial using ViewData.

  • Depending on the size of the data you want to pass around, you could also use a custom HTTP header. This would be set on the server in your action, and read on the client in JavaScript: Accessing the web page's HTTP Headers in JavaScript

Hope this helps!

Community
  • 1
  • 1
Justin Beckwith
  • 7,686
  • 1
  • 33
  • 55