I am trying to retrieve checked values but when I call controller using ajax it shows Resource could not be found

69 views Asked by At

this is my code which i have done so please help....Thank you......... any suggestion will be thankful..

This is My Action Result

[AcceptVerbs(HttpVerbs.Post)]
            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("FilterByPara",products); // <-- I Get all products which is checked here it passes the data to my view but after that it shows Resourse could not found
                        //return Json(products,JsonRequestBehavior.AllowGet );
                    }
                    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;
                }   
            }

This is My Partial View Where i call ajax to retrieve checked Data

<script type="text/javascript">

$(document).ready(function () {
    var qs = (function (a) {
        if (a == "") return {};
        var b = {};
        for (var i = 0; i < a.length; ++i) {
            var p = a[i].split('=', 2);
            if (p.length == 1)
                b[p[0]] = "";
            else
                b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
        }
        return b;
    })(window.location.search.substr(1).split('&'));
    subid = qs["SubCategoryId"];
    var originalVal;
    $('#ex2').slider().on('slideStart', function (ev) {
        originalVal = $('#ex2').data('slider').getValue();
    });
    $('#ex2').slider().on('slideStop', function (ev) {
        var newVal = $('#ex2').data('slider').getValue();
        if (newVal != null) {
            price1 = newVal[0];
            price2 = newVal[1];
        }
        RefreshItems();
    });
    $('.para').on('change', '[type=checkbox]', function (e) {
        price1= @price1;
        price2= @price2;
        RefreshItems();
    });
});



function RefreshItems() {
    var par = "";
    var chk = 0;
    var s = $(".para").toArray();
    var br = [];
    var sPara = [];
    for (var i = 0; i < s.length; i++) {
        var t = $(s[i]).attr("id");
        if ($(s[i]).attr("id") == "Brand") {
            var values = $('input:checkbox:checked.' + $(s[i]).attr("id")).map(function () {
                br.push(this.id);
            }).get();
        }
        else {
            //t = t.substring(2, t.length);
            t += "~";
            var str = "~";
            var values = $('input:checkbox:checked.' + $(s[i]).attr("id")).map(function () {
                str +=  + this.id + ",";
                sPara.push(t);
                sPara.push(this.id);
            }).get();
            str = str.substring(1, str.length);
            t += str + "^";
            par += t;
        }
    }
    
    $.ajax({
        type: "POST",
        url: '@Url.Action("FilterByPara","Home")',
        data: JSON.stringify(),            
        data: "{ 'BrandId': '" + br.join(',') +
            "', 'SubCategoryId': '" + subid + "', 'Price1': '" + price1 + "', 'Price2': '" + price2
            + "', 'SelPara': '" + sPara + "'  }",
        
        contentType: 'application/json; charset=utf-8',
        datatype: "Json",
        success: function (data) {
                  
            alert('Success');
            s = data;
            {window.location = '@Url.Action("FilterByPara","Home")'}
            
        },
        error: function (err, result) {
            alert(err.responseText + result);
        }
    });
}

And My View FilterByPara

    @model  IEnumerable<MeeraGarments.Models.Product>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
</head>
<body>
    <div>
        @{
            foreach (var item in Model)
            {
                @item.ProductsId // I retrieve ProductsId too but after pass from here it shows resource could not found..
            }
        }
    </div>
    <b>Hello</b>
</body>

</html>
0

There are 0 answers