inLine编辑模式中的剑道验证消息剑道网格无法通过数据注释

时间:2017-10-11 14:37:16

标签: c# asp.net-mvc kendo-ui kendo-asp.net-mvc

我正在使用ASP.NET MVC的kendo UI。我有一个内联编辑模式kendo网格,我的列中有一些ClientEditorTemplate DropDownLists。网格绑定到模型列表" ContractPricingPolicy"。

public class ContractPricingPolicy
{
    //Some other fields

    [CustomDisplayName(XmlResourceNames.ContractPricingPoliciesXml, "PricingPolicyType")]
    [UIHint("ClientPricingPolicyType")]
    [Required(ErrorMessage = "Tο πεδίο {0} είναι υποχρεωτικό")]
    public PricingPolicyType PricingPolicyType { get; set; }

    //Some other fields
}

正如您所看到的,我将UIHint用于EditorTemplate,并使用Required Data Annotation来验证我的字段。 编辑器模板就是这样:

@(Html.AthenaTransportDdl("GetPricingPolicyTypes", "Enterprise", HttpVerbs.Post)
    .Name("PricingPolicyType")
    .DataTextField("Label")
    .DataValueField("Id")
    .AutoBind(false)
    .Filter("contains")
    .HtmlAttributes(new { @class = "atn-flexdisplay-container" })
    .OptionLabel(Html.GetResource(AthenaWeb.Models.Common.XmlResourceNames.CommonXml, "PricingPolicyTypeOptionLabel"))
)

我的剑道网格就是这样:

@(Html.Kendo().Grid(Model.PricingPolicies)
        .Name("pricingPoliciesGrid")
        .Columns(columns =>
        {
            columns.Bound(c => c.Id).Hidden();
            columns.Bound(c => c.PricingPolicyType).Width(100)
            // Some more columns
            columns.Command(command => { command.Edit().Text(" ").UpdateText(" ").CancelText(" "); command.Destroy().Text(" "); }).Width(70)
            .HtmlAttributes(new { style = "text-align:center" });
        })
        .ToolBar(toolbar => toolbar.Template("<div class='col-md-2'><button id='addPPRowBtn' class='btn btn-success' data-atn-mouseover='cStatusDisabled'><span class='fa fa-plus'></span> " + Html.GetResource(gcw, "ToolBarInsertNew") + "</div>"))
        .Editable(editable => editable.Mode(GridEditMode.InLine).DisplayDeleteConfirmation(Html.GetResource(cpp, "DeleteConfirmationMsg")))
        .DataSource(dataSource => dataSource
            .Ajax()
            .ServerOperation(true)
            .Model(model =>
            {
                model.Id(c => c.Id);
                model.Field(p => p.PricingPolicyType).DefaultValue(new AthenaWeb.Models.Enterprise.PricingPolicyType());
                // Some other fields
            })
        .Create(create => create.Action("PricingPolicy_Create", "Student"))
        .Read(read => read.Action("PricingPolicy_Read", "Student", new { id = Model.Id }))
        .Destroy(update => update.Action("PricingPolicy_Deactivate", "Student"))
        )
    )

当我插入新行并尝试在未选择定价政策的情况下保存我的行时输入数据注释中的验证不会触发。但是,如果我将编辑器模板所需的属性设置为:

(Html.AthenaTransportDdl("GetPricingPolicyTypes", "Enterprise", HttpVerbs.Post)
    .Name("PricingPolicyType")
    .DataTextField("Label")
    .DataValueField("Id")
    .AutoBind(false)
    .Filter("contains")
    .HtmlAttributes(new { @class = "atn-flexdisplay-container", required = "required" })
    .OptionLabel(Html.GetResource(AthenaWeb.Models.Common.XmlResourceNames.CommonXml, "PricingPolicyTypeOptionLabel"))
)

验证触发但显示的消息是默认值。当我需要在我的数据注释中显示消息时,字段{NameOfField}是必需的。

我的问题是为什么会发生这种情况以及如何启用数据注释验证消息(我猜测的确切问题是如何触发绑定到编辑器模板的字段的行的kendoValidator)

1 个答案:

答案 0 :(得分:1)

要使用编辑器模板为列工作的验证,您需要在编辑器模板中添加以下内容

.HtmlAttributes(Html.GetUnobtrusiveValidationAttributes(&#34;验证&#34 ;,
 ViewData.ModelMetadata))

    (Html.AthenaTransportDdl("GetPricingPolicyTypes", "Enterprise", HttpVerbs.Post)
        .Name("PricingPolicyType")
        .DataTextField("Label")
        .DataValueField("Id")
        .AutoBind(false)
        .Filter("contains")
        .HtmlAttributes(Html.GetUnobtrusiveValidationAttributes("Validation",     
           ViewData.ModelMetadata))  
.OptionLabel(Html.GetResource(AthenaWeb.Models.Common.XmlResourceNames.CommonXml, "PricingPolicyTypeOptionLabel"))
    )