为什么我的表基于MVC中的下拉列表没有显示我想要的结果?

问题描述 投票:0回答:1

我当前正在创建MVC应用程序,我想显示一个部分页面,其中包含基于所选下拉列表的汽车列表。我还希望我的应用程序无需选择所有下拉菜单即可进行搜索。建议我使用if语句完成此操作,但不确定我是否正确执行了此操作。目前,我无法按单个下拉列表或1个下拉列表+“可用”下拉列表的任意组合进行搜索。我没有收到任何错误,但未显示任何结果。

这是我的观点

@model IgnitionHub2._0.Models.Car
@{
    ViewBag.Title = "Car Search Page";
}


<h2>Cars</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div class="center-div">
    <div class="form-inline">
        @Html.DropDownListFor(model => model.CarLotID, new SelectList(Model.CarLotList, "CarLotID", "LotName"), "Select Car Lot", new { @class = "form-control" })
        @Html.DropDownListFor(model => model.Model.MakeID, new SelectList(Model.MakeList, "MakeID", "Name"), "Select Make", new { @class = "form-control" })
        @Html.DropDownListFor(model => model.ModelID, new SelectList(Model.ModelList, "ModelID", "Name"), "Select Model", new { @id = "ModelID", @class = "form-control" })
        @Html.DropDownListFor(model => model.Available, new[] { new SelectListItem { Text = "Yes", Value = "true" }, new SelectListItem { Text = "No", Value = "false" } }, "Available", new { @id = "Available", @class = "form-control" })
        <button id="search">Search</button>
    </div>
</div>
<div id="searchResults">
    @{Html.RenderPartial("_Index", Model.CarList);}
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

    <script type="text/javascript">
    $(document).ready(
        function () {
        var makeUrl = '@Url.Action("GetCarDetails")';
        var models = $('#ModelID')
        $('#Model_MakeID').change(function () {
            models.empty();
                $.getJSON(makeUrl, { MakeID: $(this).val() },function(data){
                    if (!data) {
                        return;
                    }
                    models.append($('<option></option>').val('').text('Please select'));
                    $.each(data, function(index, item) {
                        models.append($('<option value=' + item.ModelID + '></option>').text(item.Name));
                    });
                });
            })
        })
        $(document).ready(function () {
         var url = '@Url.Action("DisplaySearchResults","Car")';
        $('#search').click(function () {
            var carLotID = $('#CarLotID').val();
            var makeID = $('#Model_MakeID').val();
            var modelID = $('#ModelID').val();
            var available = $('#Available').val();
            $('#searchResults').load(url + "?CarLotID=" + carLotID + "&MakeID=" + makeID + "&ModelID=" + modelID +"&Available=" + available);
        })
        })
    </script>
}

这是我的控制器

public ActionResult DisplaySearchResults(int? CarLotID, int? MakeID, int? ModelID, bool? Available)
    {
        if (CarLotID == null)
        {
            var makeModel = db.Cars.Where(c => c.Model.MakeID == MakeID && c.ModelID == ModelID && c.Available == Available).ToList();

           return PartialView("_Index", makeModel);
        }
        if (MakeID == null)
        {
            var calotModel = db.Cars.Where(c => c.CarLotID == CarLotID && c.ModelID == ModelID && c.Available == Available).ToList();
            return PartialView("_Index", calotModel);
        }
        if (ModelID == null)
        {
            var makeCarLot = db.Cars.Where(c => c.CarLotID == CarLotID && c.Model.MakeID == MakeID && c.Available == Available).ToList();
            return PartialView("_Index", makeCarLot);
        }
        if (Available == null)
        {
            var mmLot = db.Cars.Where(c => c.CarLotID == CarLotID && c.Model.MakeID == MakeID && c.ModelID==ModelID).ToList();
            return PartialView("_Index", mmLot);
        }
        if(Available == null & CarLotID == null)
        {
            var model = db.Cars.Where(c => c.Model.MakeID == MakeID && c.ModelID == ModelID).ToList();
            return PartialView("_Index", model);
        }
        if (Available == null && MakeID == null)
        {
            var model = db.Cars.Where(c => c.CarLotID == CarLotID && c.ModelID == ModelID).ToList();
            return PartialView("_Index", model);
        }
        if (Available == null && ModelID == null)
        {
            var model = db.Cars.Where(c => c.Model.MakeID == MakeID && c.CarLotID == CarLotID).ToList();
            return PartialView("_Index", model);
        }
        if (ModelID == null && CarLotID == null)
        {
            var model = db.Cars.Where(c => c.Model.MakeID == MakeID && c.Available == Available).ToList();
            return PartialView("_Index", model);
        }
        if (MakeID == null && CarLotID == null)
        {
            var model = db.Cars.Where(c => c.ModelID == ModelID && c.Available == Available).ToList();
            return PartialView("_Index", model);
        }
        if (MakeID == null && ModelID == null)
        {
            var model = db.Cars.Where(c => c.CarLotID == CarLotID && c.Available == Available).ToList();
            return PartialView("_Index", model);
        }
        if (ModelID == null && CarLotID == null && MakeID==null)
        {
            var model = db.Cars.Where(c => c.Available == Available).ToList();
            return PartialView("_Index", model);
        }
        if (ModelID == null && CarLotID == null && Available == null)
        {
            var model = db.Cars.Where(c => c.Model.MakeID == MakeID).ToList();
            return PartialView("_Index", model);
        }
        if (Available == null && CarLotID == null && MakeID == null)
        {
            var model = db.Cars.Where(c => c.ModelID == ModelID).ToList();
            return PartialView("_Index", model);
        }
        if (ModelID == null && Available == null && MakeID == null)
        {
            var model = db.Cars.Where(c => c.CarLotID == CarLotID).ToList();
            return PartialView("_Index", model);
        }
        if (ModelID == null && Available == null && MakeID == null &&CarLotID==null)
        {
            var model = db.Cars.ToList();
            return PartialView("_Index",model);
        }
        else
        {
            var model = db.Cars.Where(c => c.Model.MakeID == MakeID && c.ModelID == ModelID &&
            c.CarLotID == CarLotID && c.Available == Available).ToList();// build list based on parameter searchText
            return PartialView("_Index", model);
        }
    }
    public ActionResult _Index()
    {
        var cars = new List<Car>(db.Cars.Include(x => x.Images).ToList()); 
        return PartialView(cars);
    }

我没有包括此帖子的部分页面,因为它不会引起任何问题,并且我不想使帖子的长度超出其长度。

请帮助!预先谢谢你。

asp.net asp.net-mvc asp.net-mvc-4 dropdown
1个回答
0
投票

经过一些研究,我意识到这个方法是错误的。我根据搜索的内容完成了我的桌子上的期望结果,并带有以下内容:Filtering search using multiple dropdowns in MVC

© www.soinside.com 2019 - 2024. All rights reserved.