首页 > 解决方案 > 重新评估选项对验证规则没有影响

问题描述

我有一个模型,其名称字段已针对唯一值进行了远程验证。它按预期工作。现在,如果我想更新一些其他属性并且不更改名称字段,在这种情况下我也会在名称字段上收到验证错误。根据 devextreme 文档,该reevaluate选项应设置为 false 以不验证值未更改的字段。我相应地配置了验证器,但这没有效果。

下面是验证器配置脚本的视图(其他字段通过 ASP.net 验证属性配置):

@model Sender

<form asp-action=@(ViewBag.Mode == "new" ? "AddSender" : "UpdateSender") asp-controller="Sender" method="post">
    @using (Html.DevExtreme().ValidationGroup())
    {
        @Html.HiddenFor(m => Model.Id)
        @(Html.DevExtreme().Form<Sender>()
            .ID("form")
            .FormData(Model)
            .ColCount(1)
            .Items(items => {
            items.AddSimpleFor(m => Model.Name).Label(l => l.Text("Név")).Editor(e => e.TextBox().ID("sendername")).IsRequired(true);//.Editor(e => e.Autocomplete()
                //.DataSource(d => d.StaticJson().Url(Url.Action("GetNames", "Sender")))
                //.SearchMode(DropDownSearchMode.StartsWith)
                //.ValueExpr("Name"));
            items.AddSimpleFor(m => Model.Address);
            items.AddSimpleFor(m => Model.ContactPerson);
            items.AddSimpleFor(m => Model.ContactEmail);
            items.AddGroup().Items(groupItem => groupItem.AddSimple().Template(
                @<text>
                    <div style="text-align: right">
                        @(Html.DevExtreme().Button().ID("save").Text("Mentés").Width(100).Type(ButtonType.Success).UseSubmitBehavior(true))
                        @(Html.DevExtreme().Button().ID("cancel").Text("Mégsem").Width(100).Type(ButtonType.Normal).OnClick("close_onClick"))
                    </div>

                </text>));
            })
            .LabelLocation(FormLabelLocation.Top)
        )
    }
</form>
<script>
    $("#sendername").dxTextBox().dxValidator({
        validationRules: [{
            type: "async",
            message: "Partner with this name already exists",
            reevaluate: false,
            validationCallback: function (params) {
                return DevExpress.aspnet.sendValidationRequest("Name", params.value, "/MasterData/Sender/ValidateSenderName", "POST");
            }
        }, {
                type: "required",
                message: "Field value cannot be null"
           }
        ]
    });     
    
</script>

我该如何解决这个问题?

标签: javascriptvalidationdevextreme

解决方案


我建议Remote为模型的“名称”字段添加属性,而不是创建验证器。而且我在您的代码中发现了错误,您为“发件人名称”创建了 dxTextBox,但它是在 Razor 中创建的。这篇文描述了在 ASP.NET Core 和 ASP.NET MVC 中使用异步验证。


推荐阅读