我正在尝试更新具有多行的表(FlightClasses),而该行与表(Flight)一一对应,但是我无法使用更改后的值来更新它 谁能帮助我
飞行模型类别 我想更新与此表中的行链接的其他航班类别表上的行
namespace Airline.Models
{
public class FlightModel
{
public int Id { get; set; }
public string FlightName { get; set; }
public string FlightNo { get; set; }
public string OriginCity { get; set; }
public string DestinationCity { get; set; }
public DateTime Departure { get; set; }
public DateTime Arrival { get; set; }
public virtual ICollection<FlightClassesModel> FlightClasses { get; set; }
}
}
FlightClasses的模型类
namespace Airline.Models
{
public class FlightClassesModel
{
public int FlightClassesModelId { get; set; }
public type Class { get; set; }
public int AvailableSeats { get; set; }
public double Price { get; set; }
public virtual FlightModel Flight { get; set; }
public int? FlightId { get; set; }
}
public enum type
{
Business_Class,
First_Class,
Club_Class
}
}
查看
@model Airline.Models.FlightModel
@{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<h4>FlightModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="FlightName" class="control-label"></label>
<input asp-for="FlightName" class="form-control" />
<span asp-validation-for="FlightName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FlightNo" class="control-label"></label>
<input asp-for="FlightNo" class="form-control" />
<span asp-validation-for="FlightNo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="OriginCity" class="control-label"></label>
<input asp-for="OriginCity" class="form-control" />
<span asp-validation-for="OriginCity" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DestinationCity" class="control-label"></label>
<input asp-for="DestinationCity" class="form-control" />
<span asp-validation-for="DestinationCity" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Departure" class="control-label"></label>
<input asp-for="Departure" class="form-control" />
<span asp-validation-for="Departure" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Arrival" class="control-label"></label>
<input asp-for="Arrival" class="form-control" />
<span asp-validation-for="Arrival" class="text-danger"></span>
</div>
@foreach (var flightClass in Model.FlightClasses)
{
<div class="form-group">
<label asp-for="@flightClass.Class" class="control-label"></label>
<select class="form-control" asp-for="@flightClass.Class" asp-items="Html.GetEnumSelectList<type>()"></select>
<span asp-validation-for="@flightClass.Class" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@flightClass.AvailableSeats" class="control-label"></label>
<input asp-for="@flightClass.AvailableSeats" class="form-control" />
<span asp-validation-for="@flightClass.AvailableSeats" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="@flightClass.Price" class="control-label"></label>
<input asp-for="@flightClass.Price" class="form-control" />
<span asp-validation-for="@flightClass.Price" class="text-danger"></span>
</div>
}
<div class="form-group">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
控制器
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var flightModel = await _context.Flight.FindAsync(id);
if (flightModel == null)
{
return NotFound();
}
return View(flightModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, FlightModel flightModel)
{
if (id != flightModel.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(flightModel);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!FlightModelExists(flightModel.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(flightModel);
}
答案 0 :(得分:1)
此代码_context.Update(flightModel);
仅会更新flightModel,因此,如果您也想更新FlightClasses,则必须从flightModel获取FlightClasses,然后对FlightClasses中的所有实例使用_context.Update(flightClasse);
。最后,您使用await _context.SaveChangesAsync();