在列表显示的基础上进行修改
ASP.NET MVC 结合EF的列表显示
一,不带下拉框的查询用if else判断写入
1.首先在Default视图中写入显示出来的查询框与按钮
2.在后台中写入查询的参数并完善查询代码
public ActionResult Default(string Chepai = "", string Dianhua = "")
{
//创建数据上下文对象
using (var db = new CarViewModelEntity())
{
//声明listh集合
var list = new List<CarViewModel>();
//判断是否为空
if (Chepai == "" && Dianhua == "")
{
//如果为空 就显示查询对应表中所有数据并且映射到页面类上
list = db.CarRecord.Select(s => new CarViewModel()
{
Name = s.Name,
CarId = s.CarId,
State = s.State,
CarName = s.CarName,
CarNo = s.CarNo,
CarType = s.CarType,
CommunityName = s.CommunityInfo.CommunityName,
DriveTime = s.DriveTime,
Mobile = s.Mobile,
Type = s.Type
}).ToList();
}
else
{
//如果不为空就进行条件模糊查询
list = db.CarRecord.Where(w => w.CarNo.Contains(Chepai) && w.Mobile.Contains(Dianhua)).Select(s => new CarViewModel()
{
Name = s.Name,
CarId = s.CarId,
State = s.State,
CarName = s.CarName,
CarNo = s.CarNo,
CarType = s.CarType,
CommunityName = s.CommunityInfo.CommunityName,
DriveTime = s.DriveTime,
Mobile = s.Mobile,
Type = s.Type
}).ToList();
}
return View(list);
}
}
显示结果为:
二,带下拉框的查询
1.首先在Default视图中写入显示出来的查询框,下拉框与按钮
2.在后台中写入显示下拉框
public List<SelectListItem> Xiala()
{
using (var db=new CarRecordEntity())
{
var list = db.CommunityInfo.Select(s => new SelectListItem()
{
Text = s.CommunityName,
Value = s.CId.ToString(),
}).ToList();
return list;
}
}
3.写入参数并完善查询代码
public ActionResult Default(int CId = 0, string Chepai = "", string Dianhua = "")
{
}
//CId=0 表示默认值为0,会显示前台中"--所属小区--"内容
(1).用if else 判断Cid是否是否定于0
public ActionResult Default(int CId = 0, string Chepai = "", string Dianhua = "")
{
if (CId == 0)
{
//创建数据上下文对象
using (var db = new CarViewModelEntity())
{
//如果为0 就进行条件模糊查询
var list = db.CarRecord.Where(w => w.CarNo.Contains(Chepai) && w.Mobile.Contains(Dianhua)).Select(s => new CarViewModel()
{
Name = s.Name,
CarId = s.CarId,
State = s.State,
CarName = s.CarName,
CarNo = s.CarNo,
CarType = s.CarType,
CommunityName = s.CommunityInfo.CommunityName,
DriveTime = s.DriveTime,
Mobile = s.Mobile,
Type = s.Type
}).ToList();
//显示下拉框
ViewBag.mag = Xiala();
return View(list);
}
}
else
{
//创建数据上下文对象
using (var db = new CarViewModelEntity())
{
//否则 就显示查询数据并且映射到页面类上
var list = db.CarRecord.Where(w => w.CId == CId && w.CarNo.Contains(Chepai) && w.Mobile.Contains(Dianhua)).Select(s => new CarViewModel()
{
Name = s.Name,
CarId = s.CarId,
State = s.State,
CarName = s.CarName,
CarNo = s.CarNo,
CarType = s.CarType,
CommunityName = s.CommunityInfo.CommunityName,
DriveTime = s.DriveTime,
Mobile = s.Mobile,
Type = s.Type
}).ToList();
ViewBag.mag = Xiala();
return View(list);
}
}
}
(2).直接在where中进行判断
public ActionResult Default(int CId = 0, string Chepai = "", string Dianhua = "")
{
//创建数据上下文对象
using (var db = new CarViewModelEntity())
{
//判断CId是否定于0如果是就全查询,如果不是就按照下拉框返回的数据进行条件查询
var list = db.CarRecord.Where(w => (CId == 0 ? true : w.CId == CId) && w.CarNo.Contains(Chepai) && w.Mobile.Contains(Dianhua)).Select(s => new CarViewModel()
{
Name = s.Name,
CarId = s.CarId,
State = s.State,
CarName = s.CarName,
CarNo = s.CarNo,
CarType = s.CarType,
CommunityName = s.CommunityInfo.CommunityName,
DriveTime = s.DriveTime,
Mobile = s.Mobile,
Type = s.Type
}).ToList();
ViewBag.mag = Xiala();
return View(list);
}
}
显示结果为:
三,总结
用if else 判断更容易理解,但是代码量稍大。
直接在where中进行判断中稍微的难点就是(CId == 0 ? true : w.CId == CId)判断语句