如何在CheckBoxClick上调用MVC控制器并重定向到View [英] How to call MVC controller on CheckBoxClick and redirect to View

查看:50
本文介绍了如何在CheckBoxClick上调用MVC控制器并重定向到View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[HttpPost]
        public ActionResult FilterByPara(Guid SubCategoryId, string BrandId , decimal Price1 = 0, decimal Price2 = 0, string SelPara = "")
        {
            try
            {
                if (BrandId != null && BrandId != "")
                {
                    Guid[] brand = BrandId.Split(',').Select(s => Guid.Parse(s)).ToArray();
                    var products = (from p in db.Products
                                    join sc in db.SubCategories
                                    on p.SubCategoryId equals sc.SubCategoryId
                                    where sc.SubCategoryId == SubCategoryId
                                    && p.Price >= Price1 && p.Price <= Price2
                                    && brand.Any(x => x == p.BrandId)  // p.BrandId.Contains(@brand) 
                                    select p).ToList();
                    ViewBag.Subid = SubCategoryId;
                    return View(products);
                    //
                }
                else
                {
                    var products = (from p in db.Products
                                    where p.SubCategoryId == SubCategoryId
                                    select p).ToList();
                    if (products.Count > 0)
                    {
                        ViewBag.Subid = SubCategoryId;
                        return View(products);
                    }
                    else
                    {
                        return RedirectToAction("Error", "Home");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            //return Json(JsonRequestBehavior.AllowGet);
        }

推荐答案

您可以在复选框点击时添加例如事件。然后发送您的表单,这将导致调用控制器,从该控制器中的表单处理您的数据,并基于该返回您的新视图。 ;)



所以

1.在复选框上添加事件,例如jquery

2.发送表格< br $> b $ b

You can add for example event on checkbox click. And then send your form, this will result in calling controller, process your data from form in that controller and based on that return your new view. ;)

So
1. add event on checkbox using for example jquery
2. send form


#mycheckbox)。click( function (){
("#mycheckbox").click(function(){


#myForm)。submit();
})
("#myForm").submit(); })





3.控制器中的处理数据



3. process data in contoller

[HttpPost]
        public ActionResult FilterByPara(Guid SubCategoryId, string BrandId , decimal Price1 = 0, decimal Price2 = 0, string SelPara = "")







4.返回视图






4. return view

return View(products);


这篇关于如何在CheckBoxClick上调用MVC控制器并重定向到View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆