我直接将模型用于我的项目,我的主管告诉我尝试使用视图模型,因为它是现实世界中标准的编码方式。我有点理解下面代码中列出的Read函数:
public class DriverViewModel
{
public int DriverID { get; set; }
[Required]
public Nullable<int> UserID { get; set; }
public string DriverLastName { get; set; }
public string DriverFirstName { get; set; }
public string DriverLicense { get; set; }
public DateTime LicenseExpiry { get; set; }
public string MobileNumber { get; set; }
public Nullable<int> BusinessUnitID { get; set; }
public Nullable<System.DateTime> DateAdded { get; set; }
public Nullable<bool> IsActive { get; set; }
//private string fullName;
public string FullName
{
get { return DriverLastName + ", " + DriverFirstName; }
}
}
public ActionResult List()
{
var repo = new VehicleRepository();
var modelFromDB = repo.GetDrivers();
DriverListVM DVM = new DriverListVM();
DVM.Drivers = new List<DriverViewModel>();
DVM.Drivers = (from data in modelFromDB
select new DriverViewModel()
{
DriverID = data.DriverID,
LicenseExpiry = data.LicenseExpiry,
DriverLastName = data.DriverLastName,
DriverFirstName = data.DriverFirstName,
BusinessUnitID = data.BusinessUnitID,
DriverLicense = data.DriverLicense
}).ToList();
return View(DVM);
public List<Driver> GetDrivers()
{
using (var db = new VehicleManagementEntities())
{
var model = (from data in db.DriverTables.ToList()
select new Driver()
{
DriverID = data.DriverID,
DriverLicense = data.DriverLicense,
DriverLastName = data.DriverLastName,
DriverFirstName = data.DriverFirstName,
LicenseExpiry = data.LicenseExpiry,
BusinessUnitID = data.BusinessUnitID
}).ToList();
return model;
}
}
现在,我一直在尝试为一个Insert函数创建一个也会在数据库中插入两个表,但是我真的不知道如何将视图模型绑定到数据库,目前我有这个插入:
public class VehicleModel
{
public int VehicleID { get; set; }
public string Type { get; set; }
public string VehicleMake { get; set; }
public string CodingDay { get; set; }
public string Location { get; set; }
public string VehicleStatus { get; set; }
public bool IsActive { get; set; }
public int? DriverID { get; set; }
public DateTime DateAdded { get; set; }
public string DriverLastName { get; set; }
public string DriverFirstName { get; set; }
public string FullName
{
get { return FullName; }
set { FullName = DriverLastName + ", " + DriverFirstName; }
}
}
[HttpGet]
public ActionResult Add()
{
var VRepo = new VehicleRepository();
//
var getVehicles = VRepo.GetVehicles();
//
var VehicleVM = new VehicleViewModel
{
VehicleID = VehicleVM.VehicleID;
}
public List<VehicleModel> GetVehicles()
{
using (var db = new VehicleManagementEntities())
{
var model = (from data in db.Vehicles.ToList()
join data2 in db.DriverVehicles.ToList()
on data.VehicleID equals data2.VehicleID
join data3 in db.DriverTables.ToList()
on data.DriverID equals data3.DriverID
select new VehicleModel()
{
VehicleID = data.VehicleID,
Type = data.Type,
VehicleMake = data.VehicleMake,
CodingDay = data.CodingDay,
Location = data.Location,
VehicleStatus = data.VehicleStatus,
IsActive = data.IsActive,
DriverID = data.DriverID,
DateAdded = data.DateAdded,
DriverFirstName = data3.DriverFirstName,
DriverLastName = data3.DriverLastName,
}).ToList();
return model;
}
}