How should I use server side validation with ListView editor template

2 posts, 0 answers
  1. Andrei
    Andrei avatar
    5 posts
    Member since:
    Jan 2014

    Posted 14 Jul 2014 Link to this post

    I have a listview that has an editor template. I want to do server side validation (I check for uniqueness in my database) on create/update. Here is my code. Please tell me what to do to get this working.

    01.public JsonResult CreateWatch([DataSourceRequest] DataSourceRequest request, HomeWatchListItem hw) {
    02.    try {
    03.        var result = uow.HomeWatchesRepository.FindBy(w => w.SerialNo == hw.SerialNo).FirstOrDefault();
    04.        if (result != null) {
    05.            ModelState.AddModelError("SerialNo", "Serial number already exists");
    06.        }
    07.        if (ModelState.IsValid) {
    08.            uow.HomeWatchesRepository.Add(new HomeWatch {
    09.                HomeServerId = hw.IdHomeServer,
    10.                PatientId = hw.IdPatient,
    11.                SerialNo = hw.SerialNo
    12.            });
    13.            uow.Commit();
    14.        }
    15.        return Json(new [] {hw}.ToDataSourceResult(request, ModelState));
    16.    }
    17.    catch (Exception ex) {
    18.        //TODO: what to return here
    19.        return Json(new DataSourceResult {Errors = ex.Message});
    20.    }
    21.}

    When create the listView I add an error handler function that doesn't get called no matter what I do.
    01.@(Html.Kendo().ListView<HomeWatchListItem>().Name("watchesListView")
    02.    .DataSource(ds => {
    03.        ds.Read(r => {
    04.            r.Action("GetWatches", "Watches").Type(HttpVerbs.Post);
    05.        }).Create(c => {
    06.            c.Action("CreateWatch", "Watches").Type(HttpVerbs.Post);
    07.        }).Update(u => {
    08.            u.Action("EditWatch", "Watches").Type(HttpVerbs.Post);
    09.        }).Destroy(d => {
    10.            d.Action("DeleteWatch", "Watches").Type(HttpVerbs.Post);
    11.        })
    12.        .Events(e => {
    13.            e.Error("alert('a')");
    14.        })
    15.        .ServerOperation(true)
    16.        .PageSize(4)
    17.        .Model(m => {
    18.            m.Id("Id");
    19.            m.Field<int>("IdPatient");
    20.            m.Field<int>("IdHomeServer");
    21.            m.Field<string>("PatientName");
    22.            m.Field<string>("HomeServerName");
    23.        });
    24.    })
    25.    .ClientTemplateId("watch-template")
    26.    .Editable(e => {
    27.        e.TemplateName("edit-watch-template");
    28.    })
    29.    .TagName("div")
    30.    .HtmlAttributes(new { @class = "k-widget k-listview" })
    31.    .Pageable()
    32.    .Deferred()
    33.)

  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 16 Jul 2014 Link to this post

    Hello Andrei,

    Please check the sample project in this forum thread. It demonstrates how to show the server error messages when using a listview.

    Regards,
    Daniel
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top