搜索表单与级联下拉列表无法在asp.net mvc

时间:2015-09-14 07:21:54

标签: javascript c# jquery asp.net-mvc asp.net-mvc-4

我正在创建asp.net mvc 5应用程序,在该应用程序中我有模块来过滤具有多个下拉列表的结果,

结果取决于下拉

搜索表单和搜索结果显示在同一个视图页面

我已经在stackoverflow社区的帮助下实现了这个模块,

在该模块中它在第一次下拉列表中正常工作但是当我将多个下拉列表设置为锁定时,单击搜索按钮时没有结果

enter image description here

这是模型类

    public class ProductCollection
    {
        public string Product_ID { get; set; }        

        public string ProductType_ID { get; set; }
        public string Product_TypeEn { get; set; }
        public string Product_TypeAr { get; set; }

        public string ProductCategory_ID { get; set; }
        public string Product_CategoryEn { get; set; }
        public string Product_CategoryAr { get; set; }

        public string Country_ID { get; set; }
        public string Subsidary_Country { get; set; }

        public string Susidary_ID { get; set; }
        public string Susidary_NameEn { get; set; }
        public string Susidary_NameAr { get; set; }

        public string Product_Name_En { get; set; }
        public string Product_Description_En { get; set; }

        public string Product_Name_Ar { get; set; }
        public string Product_Description_Ar { get; set; }

        public string Status { get; set; }


        public string CreatedBy { get; set; }
        public Nullable<System.DateTime> CreatedDate { get; set; }
        public string UpdatedBy { get; set; }
        public Nullable<System.DateTime> UpdatedDate { get; set; }
    }

    // View models
    public class SearchVM
    {
        public string Type { get; set; }
        public string Category { get; set; }
        public string Country { get; set; }
        public string Subsidary { get; set; }
        public DateTime? Date { get; set; }
        public SelectList TypeList { get; set; }
        public SelectList CategoryList { get; set; }
        public SelectList CountryList { get; set; }
        public SelectList SubsidaryList { get; set; }
    }
    public class ProductsVM
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
    }

    // Data model  

    public class Type
    {
        public string ProductTypeID { get; set; }
        public string ProductTypeNameEn { get; set; }

   }

    public class Category
    {
        public string ProductCategoryID { get; set; }
        public string ProductCategoryNameEn { get; set; }
    }

    public class Country
    {
        public string Country_ID { get; set; }
        public string Country_Name { get; set; }
    }

    public class Subsidary
    {
        public string SubsidaryID { get; set; }
        public string SubsidaryNameEn { get; set; }
    }

这是查看页面

@model project_name.Models.SearchVM

@{
    ViewBag.Title = "Product_Search2";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Product Search</h2>

<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->

<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <!-- CSS Includes -->
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

    <style type="text/css">
        table {
            width: 100%;
            margin-top: 25px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="col-md-6 col-md-offset-3">
            <h1>Hello Stranger</h1>

            @using (Html.BeginForm())
            {
                <div class="form-group">
                    @Html.LabelFor(m => m.Type)
                    @Html.DropDownListFor(m => m.Type, Model.TypeList, "Select the type", new { @class = "form-control" })

                    @Html.LabelFor(m => m.Category)
                    @Html.DropDownListFor(m => m.Category, Model.CategoryList, "Select the category", new { @class = "form-control" })

                    @Html.LabelFor(m => m.Country)
                    @Html.DropDownListFor(m => m.Country, Model.CountryList, "Select the country", new { @class = "form-control" })

                    @Html.LabelFor(m => m.Subsidary)
                    @Html.DropDownListFor(m => m.Subsidary, Model.SubsidaryList, "Select the subsidary", new { @class = "form-control" })
                </div>

                    <button id="search" type="button" class="btn btn-success submit">Search</button>
            }

            <table>
                <thead>
                    <tr><th>ID</th><th>Product name</th><th>Category</th><th>Action</th></tr>
                </thead>
                <tbody id="table"></tbody>
            </table>

            <table id="template" style="display: none;">
                <tr><td></td><td></td><td></td><td><a>Edit</a></td></tr>
                <table>



        </div>
    </div>

    <!-- JS includes -->
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

    <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
    <script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>

    <script type="text/javascript">

            var url = '@Url.Action("FetchProducts")';
            var editUrl = '@Url.Action("Edit")';
            var type = $('#Type');

            var template = $('#template');
            var table = $('#table');
            $('#search').click(function() {
                table.empty();
                $.getJSON(url, { type: type.val(), category: category.val(), country: country.val(), subsidary: subsidary.val()}, function (data) {
                    $.each(data, function(index, item) {
                        var clone = template.clone();
                        var cells = clone.find('td');
                        cells.eq(0).text(item.ID);
                        cells.eq(1).text(item.Name);
                        cells.eq(2).text(item.Category);
                        var href = '@Url.Action("Edit")' + '/' + item.ID;
                        cells.eq(3).children('a').attr('href', href);
                        table.append(clone.find('tr'));
                    });
                });
            });



    </script>
</body>
</html>

这是控制器类

public class HomeController:Controller

    {
        public ProjectEntities db = new ProjectEntities();
        [HttpGet]
        public ActionResult Product_Search2()
        {
            var types = db.AB_ProductType.ToList();
            var categories = db.AB_ProductTypeCategory.ToList();
            var countries = db.AB_Country.ToList();
            var subsidiaries = db.AB_Subsidary.ToList();

            // ...
            SearchVM model = new SearchVM()
            {
                TypeList = new SelectList(types, "ProductTypeID", "ProductTypeNameEn"),
                CategoryList = new SelectList(categories, "ProductCategoryID", "ProductCategoryNameEn"),
                CountryList = new SelectList(countries, "Country_ID", "Country_Name"),
                SubsidaryList = new SelectList(subsidiaries, "SubsidaryID", "SubsidaryNameEn")
                // ....
            };

            return View(model);
        }


        [HttpGet]
        public JsonResult FetchProducts(string type, string category, string country, string subsidary, DateTime? date)
        {
            IEnumerable<ProductCollection> products = (from P in db.AB_Product
                                                join S in db.AB_Subsidary on P.Subsidary_ID equals S.SubsidaryID
                                                where P.Status != "Active"
                                                select new ProductCollection
                                                {
                                                    Product_ID = P.ProductID,
                                                    ProductType_ID = P.ProductTypeID,
                                                    ProductCategory_ID = P.ProductCategoryID,
                                                    Product_Name_En = P.ProductTitleEn,
                                                    Susidary_ID = P.Subsidary_ID,
                                                    Country_ID = S.Country,
                                                    CreatedDate = P.CreatedDate,
                                                    Status = P.Status

                                                }

                                      ).ToList();


            var data = products.Select(p => new
            {
                ID = p.Product_ID,
                Name = p.Product_Name_En,
                Category = p.ProductCategory_ID
            });

            return Json(data, JsonRequestBehavior.AllowGet);
        }

}

一旦我在视图页面的脚本中将$.getJSON(url, { type: type.val(), category: category.val(), country: country.val(), subsidary: subsidary.val()}, function (data) {更改为$.getJSON(url, { type: type.val()}, function (data) {这个工作,我的方法有什么问题?

2 个答案:

答案 0 :(得分:2)

我认为你错过了脚本

category ,country ,subsidiary的其他变量

传递<script></script>

中上述字段的其余值

就像你传递了Type这样的var type = $('#Type');的值,你需要传递其余的值。

在Razor中,您可以通过提供该模型属性来引用变量

ex:for m.Type可以在脚本

中用作ID #Type

答案 1 :(得分:0)

我明白了

<script type="text/javascript">

        var url = '@Url.Action("FetchProducts")';
        var editUrl = '@Url.Action("Edit")';
        var type = $('#Type');
        var category = $('#Category');
        var country = $('#Country');
        var subsidary = $('#Subsidary');

        var template = $('#template');
        var table = $('#table');
        $('#search').click(function() {
            table.empty();
            $.getJSON(url, { type: type.val(), category: category.val(), country: country.val(), subsidary: subsidary.val()}, function (data) {
                $.each(data, function(index, item) {
                    var clone = template.clone();
                    var cells = clone.find('td');
                    cells.eq(0).text(item.ID);
                    cells.eq(1).text(item.Name);
                    cells.eq(2).text(item.Category);
                    var href = '@Url.Action("Edit")' + '/' + item.ID;
                    cells.eq(3).children('a').attr('href', href);
                    table.append(clone.find('tr'));
                });
            });
        });



</script>