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.
When create the listView I add an error handler function that doesn't get called no matter what I do.
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.
)