控制器代码`:
private readonly GoodsManagementContext _context;//声明全局变量`:
public GoodsController(GoodsManagementContext context)
{
_context = context;//给全局变量赋值
}
public async Task<IActionResult> Index()//首页展示数据
{
ViewData["class"] = _context.TbClass.ToList();//给下拉列表赋值
var list = _context.Goods.ToListAsync();
return View(await list);
}
开始模糊查询和添加修改删除
/// <summary>
/// 根据条件查询数据
/// </summary>
/// <param name="GoodsName"></param>
/// <param name="IsHot"></param>
/// <param name="ClassId"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> Index(string GoodsName = "", string IsHot = "all", int ClassId = 0)
{
bool bo = false;
if (IsHot == "true")
{
bo = true;
}
else if (IsHot == "false")
{
bo = false;
}
ViewData["class"] = _context.TbClass.ToList();//给下拉列表赋值
var list = _context.Goods.Where(gc => (gc.ClassName.Contains(GoodsName) || GoodsName == "") && (gc.ClassId == ClassId || ClassId == 0) && (IsHot == "all" || gc.IsHot == bo)).ToListAsync();
return View(await list);
}
public IActionResult add()//添加数据视图页
{
ViewBag.ClassId = new SelectList(_context.TbClass.ToList(), "ClassId", "ClassName", 0);