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);
}
每当我从中删除项目时,我都想查看购物车。可以通过调用jquery函数,然后在控制器中删除ActionResult来查看此购物车。我在控制器中的脚本寄存器方法的this.Page参数中得到错误。 要调用的jquery函数如下:
<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 + "   " + 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 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;
}
答案 0 :(得分:1)
您可以这样使用JavaScriptResult返回JavaScript():
public ActionResult Deletecart(int id)
{
string script = "viewKart();";
return JavaScript(script);
}
您也可以参考this post