Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新EF core中一对多关系的数据_C#_Asp.net Core_Asp.net Core Mvc_Entity Framework Core_Razor Pages - Fatal编程技术网

C# 更新EF core中一对多关系的数据

C# 更新EF core中一对多关系的数据,c#,asp.net-core,asp.net-core-mvc,entity-framework-core,razor-pages,C#,Asp.net Core,Asp.net Core Mvc,Entity Framework Core,Razor Pages,我正试图更新一个表(FlightClasses),其中有多行与表(FlightClasses)的关系为一对多,但我无法用更改的值更新它 有人能帮我吗 飞行模型级 我想更新与此表中的行链接的FlightClass的其他表中的行 namespace Airline.Models { public class FlightModel { public int Id { get; set; } public string FlightName { get;

我正试图更新一个表(FlightClasses),其中有多行与表(FlightClasses)的关系为一对多,但我无法用更改的值更新它 有人能帮我吗

飞行模型级 我想更新与此表中的行链接的FlightClass的其他表中的行

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; }
    }
}
看法

@model Airline.Models.FlightModel
@{
ViewData[“标题”]=“编辑”;
}
编辑
飞行模型

@foreach(Model.FlightClasses中的var flightClass) { } 返回列表
控制器

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);
        }
公共异步任务编辑(int?id)
{
if(id==null)
{
返回NotFound();
}
var flightModel=wait_context.Flight.FindAsync(id);
if(flightModel==null)
{
返回NotFound();
}
返回视图(flightModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务编辑(int-id,FlightModel-FlightModel)
{
if(id!=flightModel.id)
{
返回NotFound();
}
if(ModelState.IsValid)
{
尝试
{
_更新(flightModel);
wait_context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
如果(!flightModel存在(flightModel.Id))
{
返回NotFound();
}
其他的
{
投掷;
}
}
返回重定向到操作(名称(索引));
}
返回视图(flightModel);
}

此代码
\u context.Update(flightModel)
将仅更新flightModel,因此如果您还想更新FlightClass,则必须从flightModel获取FlightClass,然后使用
\u context.update(flightClasse)用于FlightClass中的所有实例。最后使用
wait_context.SaveChangesAsync()

当我从视图中获取数据时,FlightClass显示为空。没有附加数据。我也在使用LazyLoading方法。当您在开始时获取flightModel时(在第一种方法中),FlightClass也为空?如果使用include在flightModel中获取FlightClass。但是在视图上,FlightClass的先前值可用。您的问题是表单没有在第二种方法中将FlightClass数据发送到控制器。这篇文章可能会帮助你:
@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);
        }