【问题标题】:How to call a jquery function from an ActionResult in the Controller using Mvc?如何使用 Mvc 从 Controller 中的 ActionResult 调用 jquery 函数?
【发布时间】:2014-11-11 06:44:04
【问题描述】:
  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  + " &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;
 }

【问题讨论】:

  • 您返回的不是 ActionResult 的实例...这可能适用于网络表单...
  • 是的,我在 Web 表单示例中看到了这一行代码,但它在 MVC 中不起作用
  • 它们是非常不同的框架......在您的视图中包含更多代码,也许有人会更容易提供解决方案
  • 但它在 MVC 中不起作用 那是因为它的 MVC 不是 WebForms!
  • @StephenMuecke - 嗨,斯蒂芬,我现在该怎么办? Intellisense 正在展示这些方法。有没有其他办法?

标签: jquery asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

您可以通过这种方式使用JavaScript() 返回JavaScriptResult

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

您也可以参考this post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多