ASP.NET Core 5 MVC / C#:根据另一个模型更改模型的值
ASP.NET Core 5 MVC / C#: change model's value according to another model
我是 ASP.NET Core 5 MVC 应用程序的新手。我在我的应用程序中添加了 3 个模型。它们是 Trip
、Vehicle
和 Driver
。
Vehicle
有 LastTripDateTime
、TotalTravelDistanceInKilometers
、AverageFuelConsumptionInLitres
.
Driver
有 UsedVehicleCount
。
当在 Trip
模型中添加新记录或更改任何记录时,我必须更改这些值 Vehicle
和 Driver
。
我该怎么做?这些动作应该在什么地方,我需要使用哪些方法?
我有控制器和视图。 CRUD 选项可用。我修改了 Trip 的基本脚手架创建操作,如下所示。这个有用吗?
// POST: Trips/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("TripId,VehicleId,DriverId,DistanceInKilometers,FuelConsumptionInLitres")] Trip trip)
{
if (ModelState.IsValid)
{
_context.Add(trip);
Driver driver = _context.Find<Driver>(trip.DriverId);
driver.UsedVehicleCount += 1;
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(trip);
}
您必须找到模型并更新它们
_context.Add(trip);
var driver = _context.Driver.Where(a=> a.ID == trip.DriverId).FirstOrDefault();
driver.whateveryouneedtoupdate = trip.whateverfiled;
_context.Update(driver);
var vehicle = _context.Vehicle.Where(a=> a.ID == trip.VehicleId).FirstOrDefault();
vehicle.whateveryouneedtoupdate = trip.whateverfiled;
_context.Update(vehicle)
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
你应该有两个 API 端点,一个用于
1.Adding 旅行
2.Updating旅行。
基于你的代码应该做的端点
1.Adding 旅行
context.Add(Trip);
var existingDriver = context.Driver.Where(x => x.Id == Trip.driverId).FirstOrDefault();
if (existingDriver != null)
{
existingDriver.FiedlYouwantToUpdate = existingDriver.FiedlYouwantToUpdate + Trip.FieldYouWantToUpdate;
context.Update(existingDriver);
}
else
{
context.Add(newDriver);
}
2.Updating 旅行
context.Update(Trip);
var existingDriver = context.Driver.Where(x => x.Id = Trip.driverId).FirstOrDefault();
if (existingDriver != null)
{
existingDriver.FiedlYouwantToUpdate = existingDriver.FiedlYouwantToUpdate +
Trip.FieldYouWantToUpdate;
context.Update(existingDriver);
}
else
{
context.Add(newDriver);
}
注意:你应该对两个载具都这样做,Driver
我是 ASP.NET Core 5 MVC 应用程序的新手。我在我的应用程序中添加了 3 个模型。它们是 Trip
、Vehicle
和 Driver
。
Vehicle
有 LastTripDateTime
、TotalTravelDistanceInKilometers
、AverageFuelConsumptionInLitres
.
Driver
有 UsedVehicleCount
。
当在 Trip
模型中添加新记录或更改任何记录时,我必须更改这些值 Vehicle
和 Driver
。
我该怎么做?这些动作应该在什么地方,我需要使用哪些方法?
我有控制器和视图。 CRUD 选项可用。我修改了 Trip 的基本脚手架创建操作,如下所示。这个有用吗?
// POST: Trips/Create
// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("TripId,VehicleId,DriverId,DistanceInKilometers,FuelConsumptionInLitres")] Trip trip)
{
if (ModelState.IsValid)
{
_context.Add(trip);
Driver driver = _context.Find<Driver>(trip.DriverId);
driver.UsedVehicleCount += 1;
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(trip);
}
您必须找到模型并更新它们
_context.Add(trip);
var driver = _context.Driver.Where(a=> a.ID == trip.DriverId).FirstOrDefault();
driver.whateveryouneedtoupdate = trip.whateverfiled;
_context.Update(driver);
var vehicle = _context.Vehicle.Where(a=> a.ID == trip.VehicleId).FirstOrDefault();
vehicle.whateveryouneedtoupdate = trip.whateverfiled;
_context.Update(vehicle)
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
你应该有两个 API 端点,一个用于 1.Adding 旅行 2.Updating旅行。
基于你的代码应该做的端点
1.Adding 旅行
context.Add(Trip);
var existingDriver = context.Driver.Where(x => x.Id == Trip.driverId).FirstOrDefault();
if (existingDriver != null)
{
existingDriver.FiedlYouwantToUpdate = existingDriver.FiedlYouwantToUpdate + Trip.FieldYouWantToUpdate;
context.Update(existingDriver);
}
else
{
context.Add(newDriver);
}
2.Updating 旅行
context.Update(Trip);
var existingDriver = context.Driver.Where(x => x.Id = Trip.driverId).FirstOrDefault();
if (existingDriver != null)
{
existingDriver.FiedlYouwantToUpdate = existingDriver.FiedlYouwantToUpdate +
Trip.FieldYouWantToUpdate;
context.Update(existingDriver);
}
else
{
context.Add(newDriver);
}
注意:你应该对两个载具都这样做,Driver