简体   繁体   English

从 Razor 更新列表使用 Entity Framework Core 查看

[英]Update list from Razor View using Entity Framework Core

I'm having a challenge updating from Razor View table and not sure if my approach is right.我从 Razor 视图表更新时遇到了挑战,不确定我的方法是否正确。 Instead of displaying the data (to be edited) in DIV, I'm displaying in a TABLE.我没有在 DIV 中显示数据(要编辑),而是在 TABLE 中显示。 I can understand how the hidden property is used to update the row.我可以理解隐藏属性是如何用于更新行的。 In my case I am displaying multiple rows on the Edit page, but unable to update the database for only rows that have updates.就我而言,我在“编辑”页面上显示了多行,但无法仅为具有更新的行更新数据库。

My view page is like this我的查看页面是这样的

<form method="post">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>        
    <table class="table">
        <thead>
            <tr>
                <th> @Html.DisplayNameFor(model => model.Fields[0].Name) </th>
                <th> @Html.DisplayNameFor(model => model.Fields[0].Value) </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Fields)
            {
                <tr>
                    <td> <input type="hidden" asp-for=@item.ID /> </td>
                    <td> @Html.DisplayFor(modelItem => item.Name) </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>

And the code behind is this.后面的代码是这样的。 The Fields is showing 0 in the OnPostAsync method.字段在 OnPostAsync 方法中显示为 0。

[BindProperty]
    public IList<FieldInfo> Fields { get; set; }
    public async Task<IActionResult> OnGetAsync(string id)
    {
        Fields = await _context.FieldInfo.Where(m => m.GUID == id).ToListAsync();
        return Page();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        foreach (var p in Fields) // In this approach Fields shows as count=0
        {
            _context.Attach(p.Value).State = EntityState.Modified;
        }

        _context.Attach(Fields).State = EntityState.Modified; // In this approach Exception: The entity type 'List<FieldInfo>' was not found.

        await _context.SaveChangesAsync();            
        return RedirectToPage("./Index");
    }

If you want to work with a List or collection, you must use an index to identify individual elements.如果要使用列表或集合,则必须使用索引来标识单个元素。 https://www.learnrazorpages.com/razor-pages/model-binding#binding-complex-collections https://www.learnrazorpages.com/razor-pages/model-binding#binding-complex-collections

In your case, I would use an explicit index:在您的情况下,我将使用显式索引:

@foreach (var item in Model.Fields)
{
    <tr>
        <td> <input type="hidden" asp-for="Fields[item.ID].ID" />  <input type="hidden" name="Fields.Index" value="Fields[item.ID].ID" /></td>
        <td> @item.Name</td>
        <td><textarea asp-for="Fields[item.ID].Value">@Fields[item.ID].Value">@Fields[item.ID].Value</textarea> </td>
    </tr>
}

Note that the value assigned to a td designated as contenteditable is not posted as part of a form.请注意,分配给指定为contenteditabletd的值不会作为表单的一部分发布。 You should use a suitable form control like an input or textarea instead.您应该使用合适的表单控件,例如inputtextarea

I got it working after changing the type as below, from IList to List在将类型从IList更改为List后,我得到了它的工作

[BindProperty]
public List<FieldInfo> Fields { get; set; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Entity Framework Core 更新列表 - Update list using Entity Framework Core Entity Framework Core:包括来自 SQL 视图的查找数据以显示在 Razor 页面中 - Entity Framework Core: Including lookup data from an SQL view to display in Razor Page 如何在 ASP.NET Core Razor Pages 项目的 _layout.cshtml 文件中使用 Razor 页面使用实体框架作为部分视图? - How to use Razor Page Using Entity Framework as a partial view in _layout.cshtml file in ASP.NET Core Razor Pages project? 使用实体框架更新局部实体(带有剃刀视图) - Updating partial entity using Entity Framework (with razor view) 通过Entity Framework Core将列表传递给视图 - Passing a list to a view by Entity Framework Core 在MVC Razor中使用实体框架创建动态列表 - Create a Dynamic List using Entity Framework in MVC Razor Entity Framework Core 使用 CurrentValues.SetValues 从其他实体更新实体属性,由于实体上的主键而失败 - Entity Framework Core using CurrentValues.SetValues to update entity properties from other entity failing due to primary key on entity 使用实体框架添加/更新实体列表 - Add/Update a list of entities using the entity framework 从类列表更新实体框架 - Entity Framework Update from List Of class 如何使用 Entity Framework Core 中的另一个对象更新子列表 - How to update child list with another object in Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM