jeudi 14 décembre 2017

Filter results by integer with Entity Framework

I have an MVC project and I'm using Entity Framework.

I have some data in the database that needs filtering by an integer variable (time) using checkboxes. I am getting an error message:

The parameters dictionary contains a null entry for parameter 'time' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Index(System.String, System.String, System.String[], Int32)' in 'LazyRecipe.DAL.RecipesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

How can I filter the data?

// GET: Recipes
    public ViewResult Index(string sortOrder, string searchString, string[] FilteredsearchString, int time)
    {   
        IQueryable recipes;
        if (String.IsNullOrEmpty(searchString))
        {
            recipes = db.Recipes.Include("Ingredients");
        }
        else
        {
            FilteredsearchString = searchString.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);

            // "string" can be lowercase.
            Console.WriteLine(string.Join(",", FilteredsearchString));

            // ... "String" can be uppercase.
            Console.WriteLine(String.Join(",", FilteredsearchString));

            recipes = db.Recipes.Where(r => r.Ingredients.Any(i => FilteredsearchString.Contains(i.Ingredi‌​entName)));
        }

        switch(time)
        {


            case 30:
            recipes = db.Recipes.Where(c => c.Time.CompareTo(time) <= 30);
            break;

            case 60:
            recipes = db.Recipes.Where(c => c.Time.CompareTo(time) <= 60);
            break;

            case 61:
            recipes = db.Recipes.Where(c => c.Time.CompareTo(time) >= 61);
            break;

            default:
                recipes = db.Recipes;
                break;
        }


        return View(recipes);
    }




Aucun commentaire:

Enregistrer un commentaire