使用MVC中的多个下拉列表过滤搜索可得出空模型 [英] Filtering search using multiple dropdowns in MVC gives null Model

查看:85
本文介绍了使用MVC中的多个下拉列表过滤搜索可得出空模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在构建MVC应用程序,并且尝试基于所选下拉列表显示汽车列表。我不希望用户必须选择多个下拉菜单进行搜索,我希望代码能够通过下拉菜单的任意组合进行搜索。我开始使用If else语句清洗代码,但我不认为这是最佳实践,因此我进行了一些研究,发现了另一种过滤结果的方法。我遇到的问题是我在模型中得到一个空值。

I am currently building an MVC application and I am trying to display a list of cars based on the selected dropdown. I dont want the user to have to select multiple dropdown to search, I want the code to be able to search by any combination of the dropdowns. I started wrinting the code with If else statements but I didnt think this was the best practice so I did some research and found a different way to filter my results. The issue that I am having is that the I am getting a null value in a Model.

这是我的模型

    public partial class Car 
{

    private Ignition_Hub_DBEntities Context;

    public IQueryable<Car> GetProducts(Car searchModel)
    {
        var result = Context.Cars.AsQueryable();
        if (searchModel != null)
        {
            if (searchModel.CarLotID.HasValue)
                result = result.Where(x => x.CarLotID == searchModel.CarLotID);
            if (searchModel.Available.HasValue)
                result = result.Where(x => x.Available == searchModel.Available);
            if (searchModel.ModelID.HasValue)
                result = result.Where(x => x.ModelID == searchModel.ModelID);
             if (searchModel.Model.MakeID.HasValue)
                return result;
            result = result.Where(x => x.Model.MakeID == searchModel.Model.MakeID);
            return result;
        }
        return result;
    }
        public Car()
    {
        Context = new Ignition_Hub_DBEntities();

    }
    public int? CarID { get; set; }
    public string Year { get; set; }

    public string Notes { get; set; }
    public bool? Available { get; set; }
    public string VinNumber { get; set; }
    public int? CarLotID { get; set; }
    public int? ModelID { get; set; }

    public virtual Model Model { get; set; }
    public virtual CarLot CarLot { get; set; }
}

这是我的控制人

        public ActionResult DisplaySearchResults(Car searchModel)
    {

        var business = new Car();
        var model = business.GetProducts(searchModel);
        return PartialView("_Index", model);
}

现在我在 if(searchModel .Model.MakeID.HasValue)
错误是'对象引用未设置为对象的实例。'
预先感谢您的帮助!如果有更好的方法可以做到这一点,请告知。谢谢!

Right now I get an error on if (searchModel.Model.MakeID.HasValue) the error is 'Object reference not set to an instance of an object.' Thanks in advance for the help! If there is a better way to achieve this, please advise. Thank you!

推荐答案

您需要确保 Model 属性搜索模型不为null并已传递给操作,那么在搜索时,您需要确保linq查询中元素的 Model 属性不为null,然后比较<搜索模型的code> MakeID 属性以及linq查询中元素的 MakeID 属性。例如,如果要基于 Model MakeID 搜索,如果已指定,则需要将代码更改为以下内容:

You need to make sure the Model property of the search model is not null and has been passed to the action, then when searching you need to make sure Model property of the element in linq query is not null, then compare MakeID property of the search model with MakeID property of the element in linq query. For example if you are going to search based on MakeID of the Model, if it's been specified, then you need to change the code to the following:

if (searchModel.Model !=null && searchModel.Model.MakeID.HasValue)
    result = result.Where(x => x.Model !=null && 
        x.Model.MakeID == searchModel.Model.MakeID);

但是,如果您关注此帖子,通常最好遵循以下建议:

However if you are following this post, in general it's better to follow these advises:


  • 请勿将实体模型用作POCO搜索模型。

  • 不要在POCO类中放入业务逻辑。

  • 如果要基于MakeID进行搜索,请直接将其包含在其中

示例

我将从链接文章中扩展示例,因此产品也具有类别属性,我们对此很感兴趣可以根据 CategoryName 以及其他一些属性进行搜索:

I'll extend the example from linked post, so the Product have a Category property as well and we are interested to search based on CategoryName as well as some other properties:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public int Price { get; set; }
    public string Name { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

您可以创建 ProductSearchModel 类,并根据它们放置一些要搜索的字段:

You can create a ProductSearchModel class and put some fields you want to search based on them:

public class ProductSearchModel
{
    public int? Id { get; set; }
    public int? PriceFrom { get; set; }
    public int? PriceTo { get; set; }
    public string Name { get; set; }
    public string CategoryName { get; set; }
}

然后,您可以将搜索逻辑放入 ProductBusinessLogic 这样的类:

Then you can put your search logic in ProductBusinessLogic class this way:

public class ProductBusinessLogic
{
    private YourDbContext Context;
    public ProductBusinessLogic()
    {
        Context = new YourDbContext();
    }

    public IQueryable<Product> GetProducts(ProductSearchModel searchModel)
    {
        var result = Context.Products.AsQueryable();
        if (searchModel != null)
        {
            if (searchModel.Id.HasValue)
                result = result.Where(x => x.Id == searchModel.Id);
            if (!string.IsNullOrEmpty(searchModel.Name))
                result = result.Where(x => x.Name.Contains(searchModel.Name));
            if (searchModel.PriceFrom.HasValue)
                result = result.Where(x => x.Price >= searchModel.PriceFrom);
            if (searchModel.PriceTo.HasValue)
                result = result.Where(x => x.Price <= searchModel.PriceTo);
            if (!string.IsNullOrEmpty(searchModel.CategoryName))
                result = result.Where(x => x.Category !=null && 
                    x.Category.Name.Contains(searchModel.Name));
        }
        return result;     
    }
}

这篇关于使用MVC中的多个下拉列表过滤搜索可得出空模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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