如何在MVC 5中基于静态下拉列表过滤数据库记录

时间:2018-11-22 06:28:59

标签: javascript c# ajax asp.net-mvc petapoco

我想使用静态html dropdwonlist

根据数据库列 QtyRecieved,QtyRecieved和Void 过滤数据库记录

QtyRecieved,QtyRecieved 是小数,void是布尔值

这是我尝试过的

@Html.DropDownList("Filter", new List<SelectListItem>

           {
              new SelectListItem{ Text="Open", Value = "0" },
              new SelectListItem{ Text="Partial", Value = "1" },
              new SelectListItem{ Text="All", Value = "2" }
           })

我已经使用ajax将请求发送给控制器

$("#Filter").change(function () {
    var listval = $("select option:selected").text();
        $.ajax({
            type: "GET",
            url: "@Url.Action("Index", "MaterialRequest")",
            data: { id: listval }
        });
    });

在我的控制器中,我将if条件显示为其他记录 我想,选择 Open (打开)时,它会显示记录,其中 QtyRecieved == QtyRequested

选择 Partial (部分)时,它会显示记录,其中 QtyRecieved 和Void = True

选择全部后,它将显示所有记录

请在查询中帮助我过滤记录或显示其他替代方法

public ActionResult Index(string listval)
        {
            if (listval == "Open")
            {
                ViewBag.Items = db.Query<MaterialDeptItemVw>("Select mt.MaterialRequestId, mt.TDate, d.DepartmentName, it.ItemName, mt.QtyRequested, mt.Comment, mt.RecievedDateTime , u.UnitName from MaterialRequest mt INNER JOIN Department d ON mt.DepartmentId = d.DepartmentId INNER JOIN Items it ON mt.ItemId = it.ItemId INNER JOIN Units u ON it.UnitId = u.UnitId where QtyRecieved = QtyRequested");
            }
            else if (listval == "Partial")
            {
                ViewBag.Items = db.Query<MaterialDeptItemVw>("Select mt.MaterialRequestId, mt.TDate, d.DepartmentName, it.ItemName, mt.QtyRequested, mt.Comment, mt.RecievedDateTime , u.UnitName from MaterialRequest mt INNER JOIN Department d ON mt.DepartmentId = d.DepartmentId INNER JOIN Items it ON mt.ItemId = it.ItemId INNER JOIN Units u ON it.UnitId = u.UnitId where QtyRecieved < QtyRequested and Void = 0");
            }
            else if (listval == "All")
            {
                ViewBag.Items = db.Query<MaterialDeptItemVw>("Select mt.MaterialRequestId, mt.TDate, d.DepartmentName, it.ItemName, mt.QtyRequested, mt.Comment, mt.RecievedDateTime , u.UnitName from MaterialRequest mt INNER JOIN Department d ON mt.DepartmentId = d.DepartmentId INNER JOIN Items it ON mt.ItemId = it.ItemId INNER JOIN Units u ON it.UnitId = u.UnitId");
            }

            return View();
        }

1 个答案:

答案 0 :(得分:1)

您的代码中有几个问题:

1)在AJAX回调中,您拥有public ActionResult Index(string listval)参数时声明为data: { id: listval }的操作方法,因此AJAX调用从未到达控制器操作,因为它使用了不同的参数名称。

2)return View()在使用AJAX时不适用,必须返回JSON数据或部分视图并从AJAX结果中更新目标DOM元素。

因此,您应该将参数名称更改为与AJAX data参数完全匹配,然后使用return Json()

public ActionResult Index(string id)
{
    // using DRY principle, just write the same part of the query in a string variable
    // and add another part depending on case value inside switch block below
    string baseQuery = @"Select mt.MaterialRequestId, mt.TDate, 
                         d.DepartmentName, it.ItemName, 
                         mt.QtyRequested, mt.Comment, 
                         mt.RecievedDateTime , u.UnitName from MaterialRequest mt 
                         INNER JOIN Department d ON mt.DepartmentId = d.DepartmentId 
                         INNER JOIN Items it ON mt.ItemId = it.ItemId 
                         INNER JOIN Units u ON it.UnitId = u.UnitId";

    switch (id)
    {
        case "Open":
            baseQuery += " where QtyRecieved = QtyRequested";
            break;

        case "Partial":
            baseQuery += " where QtyRecieved < QtyRequested and Void = 0";
            break;

        case "All":
            break; // not doing anything

        default: goto case "All";
    }

    // create a list of object from query results
    var items = db.Query<MaterialDeptItemVw>(baseQuery).ToList();

    // return JSON data to populate target element
    return Json(items, JsonRequestBehavior.AllowGet);
}

然后修改AJAX调用以根据返回的数据更新目标DOM元素:

$("#Filter").change(function () {
    var listval = $("select option:selected").text();
    $.ajax({
        type: "GET",
        url: '@Url.Action("Index", "MaterialRequest")',
        data: { id: listval },
        success: function (result) {
           // an example to update target element
           $('#targetElementID').html(result);
        },
        error: function (xhr, status, err) {
           // error handling
        }
    });
});