【问题标题】:ASP.NET Core 5 MVC / C#: change model's value according to another modelASP.NET Core 5 MVC / C#:根据另一个模型更改模型的值
【发布时间】:2021-01-24 13:04:32
【问题描述】:

我是 ASP.NET Core 5 MVC 应用程序的新手。我在我的应用程序中添加了 3 个模型。它们是TripVehicleDriver

VehicleLastTripDateTimeTotalTravelDistanceInKilometersAverageFuelConsumptionInLitres

DriverUsedVehicleCount

当在Trip 模型中添加新记录或更改任何记录时,我必须从VehicleDriver 更改这些值。

我该怎么做?这些动作应该在哪里,我需要使用哪些方法?

我有控制器和视图。 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);
    }

【问题讨论】:

    标签: c# entity-framework-core asp.net-core-mvc .net-5


    【解决方案1】:

    您应该有两个 API 端点,一个用于 1.增加行程 2.更新行程。

    基于你的代码应该做的端点

    1.添加行程

        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.更新行程

        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);
        }
    

    注意:您应该对车辆和司机都这样做

    【讨论】:

      【解决方案2】:

      你必须找到模型并更新它们

       _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));
      

      【讨论】:

        猜你喜欢
        • 2021-11-14
        • 1970-01-01
        • 2018-11-07
        • 1970-01-01
        • 1970-01-01
        • 2019-12-03
        • 1970-01-01
        • 2020-12-03
        • 1970-01-01
        相关资源
        最近更新 更多