1
  public  ActionResult Deletecart(int id)
    {

        cartList = (List<Product>)System.Web.HttpContext.Current.Application["cartList"];
        Product p = cartList.SingleOrDefault(item => item.ProductId == id);
        cartList.Remove(p);
        System.Web.HttpContext.Current.Application["cartList"] = cartList;
        int cartLen = cartList.Count;
        System.Web.HttpContext.Current.Application["CartLen"] = cartLen;
        //*** xxx  *//

       return ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "viewKart();", true);            

    }

I want to view the cart whenever I delete the items from it. This cart can be viewed by calling the jquery function followed by the delete ActionResult in the controller. I am gettinmg the error in this.Page argument of the script register method in controller. The jquery function to be called is as follows :

<script type="text/javascript">
//alert("hello");
function viewKart() {
    // alert("hello");
    $("#table").empty();
    debugger;
    $.getJSON('@Url.Action("ViewCart", "home")',
     function (data) {
         debugger;

         if (data == "" || data == null) {

             $(window).scrollTop(0);
             $("#table").append("<h2> No results found ! </h2>");

         }
         if (data != null) {
             $.each(data, function (index, item) {


                 var len = data.length;
                 alert(len);
                 var txt = "";
                 if (len > 0) {


                     for (var i = 0; i < len; i++) {

                         if (data[i].ProductId && data[i].Name && data[i].ShortDescription && data[i].MediumImage && data[i].Price && data[i].IconImage) {
                             //alert(data)
                             //var date = new Date(parseInt(data[i].date.substr(6)));
                             var Photoq = "/Images/HomeImages/" + data[i].MediumImage;

                             //alert(Photoq);
                             //<img id="imgAd" src="/Images/HomeImages/1.jpg" width="181px" height="215px" alt="img">
                             var Photo = "<img id='imgAd' src='" + Photoq + "' width='100px' height='100px' alt='img'/>";

                             //alert(Photo);

                             txt += '<tr><td><div id ="result1" ><div>' + Photo + '</div> <div ><div>' + '<div id="hello">' + data[i].ProductId + '</div>' + "</br> Name- " + data[i].Name + "</br> Description " + data[i].ShortDescription + ", </br>" +'<div class="totals">'+ data[i].Price+'</div>' + '<button class="Btnremove" type="button" data-id="' + data[i].ProductId + '">Remove</button>' + "</br>";
                             //txt += data[i].ProductId + Photo  + " &nbsp " + data[i].Name + " &nbsp " + data[i].ShortDescription +"&nbsp" + data[i].Price+"</br>" ;

                         }

                         $(document).on('click', ".Btnremove", function (event) {
                             debugger;
                             var id = $(this).data('id');
                             $(this).closest('tr').removeData();
                             alert('ashj')
                             debugger;
                             $.getJSON('@Url.Action("Deletecart", "home")', {
                                 id: $(this).data('id')
                             }, location.reload(true), function (data) {
                                 if (data == null) {
                                     alert('Cart is empty');
                                 }

                             });



                             @*$.getJSON('@Url.Action("Deletecart", "home")', {
                                id: $(this).data('id')
                             },location.reload(true), function (data) {

                            });*@
                         });                             



                     }
                     if (txt != "") {
                         $("#table").append(txt);

                     }

                 }
                 return false;
             });

         }

     })
     $("#popupdiv").dialog({
         title: "AddCart",
         width: 630,
         height: 450,
         modal: true,
         buttons: {
             Close: function () {
                 $(this).dialog('close')
             }
         }
     })



        //$("#popupdiv").dialog("open")
     return false;
 }

12
  • What you are returning is not an instance of ActionResult... this would probably have worked in webforms ... Commented Nov 11, 2014 at 6:47
  • Yes, I saw this one line code in web forms example but its not working in the MVC Commented Nov 11, 2014 at 6:49
  • They are very different frameworks .. include more of the code in your view and maybe it'll be easier for someone to provide a solution Commented Nov 11, 2014 at 6:50
  • 2
    but its not working in the MVC Thats because its MVC not WebForms! Commented Nov 11, 2014 at 6:50
  • @StephenMuecke - Hi stephen, What should I do now ? Intellisense is showing these methods. Is there any other way ? Commented Nov 11, 2014 at 6:52

1 Answer 1

1

You can return JavaScriptResult using JavaScript() this way:

public  ActionResult Deletecart(int id)
{
   string script = "viewKart();";
   return JavaScript(script);
}

You can also refer this post

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.