【发布时间】:2018-05-28 10:50:39
【问题描述】:
我有一个 MVC 项目,我正在使用实体框架。
我在数据库中有一些数据需要使用复选框通过整数变量(时间)进行过滤。我收到一条错误消息:
参数字典包含方法'System.Web.Mvc.ViewResult Index(System.String, System.String, System.String[]的不可空类型'System.Int32'的参数'time'的空条目, Int32)' 在'LazyRecipe.DAL.RecipesController' 中。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数
如何过滤数据?
// 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.IngredientName)));
}
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);
}
【问题讨论】:
-
该消息是不言自明的 - 您没有为
time参数传递值。 -
Where(c => c.Time.CompareTo(time) <= 30)- 我想你的意思是Where(c => c.Time <= 30);? -
是的,这就是我真正想要的!我应该在 switch 中传递什么?
标签: c# asp.net-mvc entity-framework checkbox filtering