首页 > 解决方案 > 为什么批量更新的 KendoUI Grid 中的 Id 属性上的模型状态无效?

问题描述

我的 ASP.NET Core Web 应用程序中有一个 KendoUI 网格,它对其操作使用批量更新。出于某种原因,当我保存更改时,网格没有正确更新。我调查了控制器,发现模型状态返回无效。在仔细检查该错误时,失败的字段是“Id”。

我查看了在我的操作上使用断点传入的模型内容,我可以看到 Id 设置为“0”,这是我希望看到的,因为这些是正在创建的新记录。那么,为什么 id 模型验证失败?

这是我的代码:

网格

请注意:@localId 是模型 ID(视图的),它在这里用作附加参数,因为我们没有其他方法可以将其传递到网格中,因为网格与视图是分开的,因此不知道 Id我们需要抓住的。

<kendo-grid name="requirement_position_periods_@localId">
        <datasource type="DataSourceTagHelperType.Ajax" page-size="5" batch="true" server-operation="false">
            <transport>
                <read url="/Test/ReadPeriods" data="getClientDataModel(@localId)" />
                <create url="/Test/AddPeriod" data="getClientDataModel(@localId)" />
            </transport>
            <schema>
                <model id="Id">
                    <fields>
                        <field name="Id" editable="false" />
                        <field name="Commencement" editable="true" type="date" />
                        <field name="RequirmentId" editable="true" type="number" />
                        <field name="Fixed" editable="false" />
                    </fields>
                </model>
            </schema>
        </datasource>
        <toolbar>
            <toolbar-button template="period_heading"></toolbar-button>
            <toolbar-button name="create" text=" " icon-class="k-icon k-i-plus-outline"></toolbar-button>
            <toolbar-button name="save" text=" " icon-class="k-icon k-i-save"></toolbar-button>
            <toolbar-button name="cancel" text=" " icon-class="k-icon k-i-cancel"></toolbar-button>
        </toolbar>
        <editable mode="incell" />
        <sortable enabled="true" />
        <scrollable enabled="false" />
        <filterable enabled="false" />
        <columns>

            <column selectable="true" hidden="true" width="20" template="<label class='check-container'><input type='checkbox' /><span class='checkmark'></span></label>"></column>
            <column field="Id" title="Id" hidden="true" />
            <column field="Commencement" title="Commencement" format="{0:dd/MM/yyyy}" />            
            <column field="Duration" title="Duration" />
            <column field="Frequency" title="Frequency" />    
        </columns>
    </kendo-grid>

FixtureOption.cs

{
    public class RequirementOption : BaseEntity
    {
        public int RequirementId { get; set; }
        public int PeriodId { get; set; }
        public int? Duration { get; set; }
        public string Frequency { get; set; }
    }
}

添加期

int Id作为附加参数传入,它总是有一个值。删除它不会改变我面临的问题。

[HttpPost]
public IActionResult AddPeriod([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")] IEnumerable<RequirementPeriod> periods, int Id)
        {
            var results = new List<RequirementPeriod>();

            if (periods != null)
            {
                foreach (var period in periods)
                {
                    period.RequirementId = Id;
                    if (ModelState.IsValid)
                    {
                        try
                        {
                            _requirementPeriodService.InsertRequirementPeriod(period);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    results.Add(period);
                }
            }

            return Json(results.ToDataSourceResult(request, ModelState));
        }

错误 断点处的错误。

我不确定为什么 Id 为 0 失败,但它确实失败了,我需要一些帮助来整理它,因为尽管添加了记录,但网格没有正确更新。

标签: asp.net-corekendo-uientity-framework-core

解决方案


推荐阅读