asp.net mvc - 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 view cart whenever delete items it. cart can viewed calling jquery function followed delete actionresult in controller. gettinmg error in this.page argument of script register method in controller. jquery function called 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) { (var = 0; < 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 + "   " + data[i].name + "   " + data[i].shortdescription +" " + 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 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() way:
public actionresult deletecart(int id) { string script = "viewkart();"; return javascript(script); }
you can refer this post
Comments
Post a Comment