C# 使用实体框架核心更新列表

C# 使用实体框架核心更新列表,c#,entity-framework-core,C#,Entity Framework Core,我有两门课,Student和Details public class Student { [Key] public int StudentD { get; set; } // PK public string StudentName { get; set; } public string StudentType { get; set; } public virtual ICollection<Details> Details { get; se

我有两门课,
Student
Details

public class Student
{
    [Key]
    public int StudentD { get; set; } // PK
    public string StudentName { get; set; }
    public string StudentType { get; set; }
    public virtual ICollection<Details> Details { get; set; } // FK
}
public class Details
{
    [Key]
    public int DetailsID { get; set; } // PK
    public string Property { get; set; }
    public string Value { get; set; }
    public virtual Student Student { get; set; }
    public int StudentID { get; set; } // FK
}
公共班级学生
{
[关键]
公共int StudentD{get;set;}//PK
公共字符串StudentName{get;set;}
公共字符串StudentType{get;set;}
公共虚拟ICollection详细信息{get;set;}//FK
}
公开课详情
{
[关键]
public int DetailsID{get;set;}//PK
公共字符串属性{get;set;}
公共字符串值{get;set;}
公共虚拟学生学生{get;set;}
public int StudentID{get;set;}//FK
}
我在网页上显示所有学生的列表,我希望允许编辑所选学生的学生详细信息

<form method="post">
<table class="table">
    <thead>
        <tr>
            <th></th>
            <th> @Html.DisplayNameFor(model => model.Details[0].Name) </th>
            <th> @Html.DisplayNameFor(model => model.Details[0].Value) </th>
        </tr>
    </thead>
    <tbody>
    @foreach (var item in Model.Details)
    {
        <tr>
        <td> <input type="hidden" asp-for=@item.DetailsID /> </td>
        <td> @Html.DisplayFor(modelItem => item.Property) </td>
        <td contenteditable='true'> @Html.DisplayFor(modelItem => item.Value) </td>
        </tr>
    }
    </tbody>
</table>
<div class="form-group">
    <input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>

@Html.DisplayNameFor(model=>model.Details[0].Name)
@DisplayNameFor(model=>model.Details[0].Value)
@foreach(模型中的var项。详细信息)
{
@DisplayFor(modelItem=>item.Property)
@DisplayFor(modelItem=>item.Value)
}
页面模型如下所示

public class EditModel : PageModel
{
    private readonly TestEFCore.Data.TestEFCoreContext _context;
    public EditModel(TestEFCore.Data.TestEFCoreContext context)
    {
        _context = context;
    }

    [BindProperty]
    public IList<TestEFCore.Models.Details> Details { get; set; }

    public async Task<IActionResult> OnGetAsync(int? id)
    {
        if (id == null) { return NotFound(); }
        Detailss = await _context.Details.Where(m => m.StudentID == id).ToListAsync();
        return Page();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid) { return Page(); }

        _context.Attach(Details).State = EntityState.Modified;

        try { await _context.SaveChangesAsync(); }
        catch (DbUpdateConcurrencyException) { throw; }

        return RedirectToPage("./Index");
    }
}
公共类EditModel:PageModel
{
私有只读TestEFCore.Data.TestEFCoreContext\u上下文;
公共编辑模型(TestEFCore.Data.TestEFCoreContext上下文)
{
_上下文=上下文;
}
[BindProperty]
公共IList详细信息{get;set;}
公共异步任务OnGetAsync(int?id)
{
如果(id==null){return NotFound();}
Detailss=wait_context.Details.Where(m=>m.StudentID==id).toListSync();
返回页();
}
公共异步任务OnPostAsync()
{
如果(!ModelState.IsValid){return Page();}
_context.Attach(Details).State=EntityState.Modified;
请尝试{wait _context.SaveChangesAsync();}
catch(DbUpdateConcurrencyException){throw;}
返回页首(“/索引”);
}
}
当一个学生被选中时,我会在一个HTML表中显示详细信息,并希望EF Core跟踪HTML表中的更改。为了实现这一点,我创建了一个
IList
Details
并显示它,但是当用户更新任何值时,我得到一个错误,即
IList
在我可以理解的模型中不存在,因为模型只有DB表列信息,而没有行的
IList


有人能建议我如何实现这一点吗?

我在将类型从
IList
更改为
List
后,使其正常工作

[BindProperty]
public List<TestEFCore.Models.Details> Details { get; set; }
[BindProperty]
公共列表详细信息{get;set;}

为了清晰起见,我添加了示例测试代码