简体   繁体   English

如何从 controller 的角度将数据传递给 jquery

[英]How to pass data to jquery in view from controller

I've a Viewbag in my controller.我的 controller 中有一个 Viewbag。 And i want to reach it from jquery code in view.我想从 jquery 代码中看到它。 Is it possible?可能吗? I want to check my customer's money about if it is enough for buy the product.我想检查客户的钱是否足以购买产品。 If they don't have money enough i want to show them a sweet alert.如果他们没有足够的钱,我想向他们展示一个甜蜜的警报。 Is it possible and if it is how can i do it?有可能吗?如果可以,我该怎么做?

That's my [GET] controller;那是我的 [GET] controller;

 public ActionResult inspect(string id, int page = 1)
    {
        if (id == null)
        {
            return RedirectToAction("account", "product");
        }
        ViewBag.href = id;
        ViewBag.user = Session["MAIL"];
        var valuehrefid = db.TBLPRODUCT.Where(x => x.href == id).Select(y => y.ID).FirstOrDefault();
        var accounts = db.TBLACCOUNT.Where(x => x.PRODUCT == valuehrefid && x.status == true).ToList().ToPagedList(page, 10);
        return View(accounts);
    }

That's my [POST] controller;那是我的 [POST] controller;

public ActionResult buyaccount(int id)
    {
        var mail = (string)Session["MAIL"];
        var userid = db.TBLUSER.Where(x => x.MAIL == mail).Select(y => y.ID).FirstOrDefault();
        var price = db.TBLACCOUNT.Where(x => x.ID == id).Select(y => y.PRICE).FirstOrDefault();
        var user = db.TBLUSER.Find(userid);
        if (user == null || mail == null)
        {
            ViewBag.alert = "usernull";
            return RedirectToAction("login", "home");
        }
        var href = db.TBLACCOUNT.Where(x => x.ID == id).Select(y => y.TBLPRODUCT.href).FirstOrDefault();
        var customercash = user.CASH;
        if (customercash >= price)
        {
            user.CASH -= price;
            var value = db.TBLACCOUNT.Find(id);
            value.status = false;
            var product = db.TBLACCOUNT.Where(x => x.ID == id).Select(y => y.TBLPRODUCT.ID).FirstOrDefault();
            var productd = db.TBLPRODUCT.Find(urun);
            product.STOCK -= 1;
            var date = DateTime.Parse(DateTime.Now.ToShortDateString());
            TBLSALEACTION p = new TBLSALEACTION();
            p.CUSTOMER = userid;
            p.ACCOUNT = id;
            p.PRODUCT = product;
            p.DATE = date;
            p.PRICE = price;
            db.TBLSALEACTION.Add(p);
            db.SaveChanges();
            ViewBag.Uyari = "successful";
        }
        else
        {
            ViewBag.Uyari = "Customer's money is not enough to buy this.";
            int sayfa = 1;
            var valuehrefid = db.TBLPRODUCT.Where(x => x.href == href).Select(y => y.ID).FirstOrDefault();
            var accounts = db.TBLACCOUNT.Where(x => x.PRODUCT == valuehrefid && x.status == true).ToList().ToPagedList(sayfa, 10);
            return View("inspect", model: hesaplar);
        }
        return View("~/Views/accounts/inspect/" + href + ".cshtml", href);
    }

And that's my view这就是我的观点

   @if (Session["MAIL"] != null)
        {
    <script>
                $('#TBLACCOUNT').on("click", ".btnBuy", function () {
                    var btn = $(this);
                    Swal.fire({
                        title: 'Warning',
                        text: "Are you sure to buy this?",
                        type: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: 'Yes',
                        cancelButtonText: 'No'
                    }).then((result) => {
                        if (result.value) {
                            var id = btn.data("id");
                            $.ajax({
                                type: "POST",
                                url: "/accounts/buyaccount/" + id,
                                success: function (s) {
                                    if (s) {
                                btn.parent().parent().remove();
                                Swal.fire(
                                    'Information ',
                                    'You have successfully purchased!',
                                    'success'
                                );
                                    } else {
                                        Swal.fire({
                                            type: 'error',
                                            title: 'Error.',
                                            text: 'You dont have enough money.',
                                            confirmButtonText: 'Close'
                                        });
                                    }
                                }
                            });
                        }
                    });
                });
    </script>
        }

add this class:添加此 class:

public class JsonData
{
    public string HtmlUrl{ get; set; }
    public string HtmlBody { get; set; }
    public bool Success { get; set; }
}

then in controller returned Json value:然后在 controller 返回 Json 值:

public ActionResult buyaccount(int id)
    {
        var mail = (string)Session["MAIL"];
        var userid = db.TBLUSER.Where(x => x.MAIL == mail).Select(y => y.ID).FirstOrDefault();
        var price = db.TBLACCOUNT.Where(x => x.ID == id).Select(y => y.PRICE).FirstOrDefault();
        var user = db.TBLUSER.Find(userid);
        if (user == null || mail == null)
        {
             return Json(new JsonData()
            {
                HtmlUrl = "/home/login",
                HtmlBody = "usernull",
                Success = false,
            });
        }
        var href = db.TBLACCOUNT.Where(x => x.ID == id).Select(y => y.TBLPRODUCT.href).FirstOrDefault();
        var customercash = user.CASH;
        if (customercash >= price)
        {
            user.CASH -= price;
            var value = db.TBLACCOUNT.Find(id);
            value.status = false;
            var product = db.TBLACCOUNT.Where(x => x.ID == id).Select(y => y.TBLPRODUCT.ID).FirstOrDefault();
            var productd = db.TBLPRODUCT.Find(urun);
            product.STOCK -= 1;
            var date = DateTime.Parse(DateTime.Now.ToShortDateString());
            TBLSALEACTION p = new TBLSALEACTION();
            p.CUSTOMER = userid;
            p.ACCOUNT = id;
            p.PRODUCT = product;
            p.DATE = date;
            p.PRICE = price;
            db.TBLSALEACTION.Add(p);
            db.SaveChanges();
        }
        else
        {
            
            int sayfa = 1;
            var valuehrefid = db.TBLPRODUCT.Where(x => x.href == href).Select(y => y.ID).FirstOrDefault();
            var accounts = db.TBLACCOUNT.Where(x => x.PRODUCT == valuehrefid && x.status == true).ToList().ToPagedList(sayfa, 10);
            return Json(new JsonData()
            {
                HtmlUrl = "/accounts/inspect/hesaplar",
                HtmlBody = "Customer's money is not enough to buy this.",
                Success = false,
            });
        }
        
         return Json(new JsonData()
            {
                HtmlUrl = "/accounts/inspect/href",
                HtmlBody = "successful",
                Success = true,
            });
    }

and in script:并在脚本中:

if (s.Success) {
        btn.parent().parent().remove();
        Swal.fire(
              'Information ',
              'You have successfully purchased!',
              'success'
           );
           window.location.href = s.HtmlUrl;
} else {
        Swal.fire({
        type: 'error',
        title: 'Error.',
        text: s.MsgBody,
        confirmButtonText: 'Close'
    });
    window.location.href = s.HtmlUrl;
}

You can not transfer the program to another view with an ajax request, but you have to go back to the desired page with javascript depending on the situation.您不能使用 ajax 请求将程序转移到另一个视图,但您必须根据情况使用 javascript 将 go 返回到所需的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 controller 传递数据以在 MVC 中的 JQuery AJAX 调用期间查看? - How to pass data from a controller to view during a JQuery AJAX call in MVC? 如何从视图接收数据到 Controller 然后传递到新视图 - How to receive data from a View to a Controller and then pass to a new View 如何使用jQuery将对象从MVC控制器动作传递到视图 - How to pass an object from a mvc controller action to a view using jquery 如何从 controller 传递数据以从带有参数的 post 方法查看? - How to pass data from controller to view from post method with parameter? 如何将按钮值从视图传递到控制器以过滤数据 - How to pass the button value from view to controller in order to filter data 如何将数据从控制器传递到 ASP.NET MVC 中查看 - How to to pass data from controller to view in ASP.NET MVC 如何将整个模型(带有数据)从视图传递到控制器? - how to pass entire model (with data) from view to controller? 如何将变量数据从 controller 传递到视图(MVC) - How to pass a variables data from a controller to a view (MVC) 如何从控制器到视图传递一行数据,而不是IEnumerable &lt;&gt; - How to pass one row of data from controller to view , not as IEnumerable<> 如何在MVC中使用IEnumerable模型将数据从控制器传递到视图? - How to pass Data from Controller to View with IEnumerable Model in MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM