简体   繁体   中英

How to call a jquery function from an ActionResult in the Controller using Mvc?

  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;
 }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM