使用kendo UI验证唯一的表单字段

时间:2014-04-03 11:37:10

标签: asp.net-mvc-3 kendo-ui

我有一个带有Kendo UI的表单。我希望在提交表单时输入名称是唯一的。 如何使用Kendo UI设置此验证? 还有与Kendo UI或控制器操作相关的验证吗?

这是代码:

 @(Html.Kendo().Grid(Model)
    .Name("GridConstante")
    .HtmlAttributes(new { style = "width:900px;height:380px" })
    .Editable(editing => 
        {
            editing.Mode(GridEditMode.InCell);
            editing.DisplayDeleteConfirmation("Êtes-vous sûr de vouloir supprimer cette constante ?");
        })
    .ToolBar(commands =>
    {
        commands.Save().CancelText("Annuler").SaveText("Valider").HtmlAttributes(new { style = "width:100px;float:right" });
        commands.Create().Text("Ajouter").HtmlAttributes(new { style = "width:100px;float:right" });

    })

    .DataSource(dataSource => dataSource
            .Ajax()
            .Read("Liste", "Constante")
            .Create("Save", "Constante")
            .Update("Save", "Constante")
            .Destroy("Delete", "Constante")
           .Model(model => model.Id(p => p.ConstanteId))
           .Events(e => e.RequestEnd("OnKendoGridAdministrationSaveEnd"))
            )

    .Columns(columns =>
    {
        columns.Bound(p => p.ConstanteId).Groupable(false).Hidden();
        columns.Bound(p => p.NOM).Width(100).Title("Constante") ;
        columns.Bound(p => p.LIBELLE).Width(250).Title("Libellé");
        columns.ForeignKey(p => p.KIND, ViewData["ConstanteType"] as SelectList).Width(35).Title("Type").HtmlAttributes(new { style = "text-align:center" }) ;
        columns.Bound(p => p.VALE).Width(80).Title("Valeur").Format("{0:N3}").HtmlAttributes(new { style = "text-align:right" }); 
        columns.Command(command => command.Destroy().Text("supprimer")).Width(80).Title("Action");
    })
    .Scrollable()
            .Selectable(selectable => selectable
                .Mode(GridSelectionMode.Single)
                .Type(GridSelectionType.Row))
    .Navigatable()
    .Events(e => e.Remove(@"function(e){
                    setTimeout(function(){
                        $('#GridConstante').data('kendoGrid').dataSource.sync()
                    }
                    )}"))
  )

请告诉我如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

Kendo网格无法判断该值是否唯一。这是您必须在Save操作中在控制器中编程的内容。

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save([DataSourceRequest] DataSourceRequest request, Model m)
{
    if (m != null && ModelState.IsValid)
    {
        //perform validation stuff here.
        //If the name is not valid, add an error to the ModelState.
        ModelState.AddModelError("NOM", "Your error text");

    }            

    //and return the model state along with the Json
    return Json(new[] { m }.ToDataSourceResult(request, ModelState));
}    

以下是剑道网站上的自定义验证示例:http://demos.telerik.com/kendo-ui/web/grid/editing-custom-validation.html

相关问题