I am having problems with InLine add and update. If I enter a item that fails the server side validation then the item is still shown as entered/updated. I have followed your examples and looked for threads on this issue, but only found things on PopUp which don't seem to apply here:
The code for add in MVC4 is given below (note: it fails on the ModelState.IsValid, which is correct
My razor view is:
Your help on this matter would be appreciated.
The code for add in MVC4 is given below (note: it fails on the ModelState.IsValid, which is correct
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult AjaxServiceCreate([DataSourceRequest] DataSourceRequest request, ISmServiceDto newItem, ICreateSmService service)
{
if (newItem != null && ModelState.IsValid)
{
var response = service.Create(newItem);
if (!response.IsValid)
response.CopyErrorsToModelState(ModelState);
}
return Json(new[] { newItem }.ToDataSourceResult(request, ModelState));
}
@model bool
@{
ViewBag.Title = "Services";
}
<
h2
>Services</
h2
>
@*<
div
id
=
"message"
class
=
"Message"
></
div
>*@
@Html.AntiForgeryToken()
@(Html.Kendo().Grid<
ServiceLayer.Models.DTOs.SmServiceDto
>()
.Name("Services")
.Columns(columns =>
{
columns.Bound(p => p.SmServiceId).Hidden();
columns.Bound(p => p.ShortName);
columns.Bound(p => p.FullName);
columns.Bound(p => p.Locked);
if (@Model)
{
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(172);
}
})
.ToolBar(toolbar =>
{
if (@Model)
{
toolbar.Create();
}
})
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
//.Scrollable()
//.HtmlAttributes(new {style = "height:430px;"})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Events(events => events.Error("error_handler"))
.Model(model =>
{
model.Id(p => p.SmServiceId);
model.Field(x => x.SmServiceId).Editable(false);
model.Field(x => x.Locked).Editable(false);
})
.Create(x => x.Action("AjaxServiceCreate", "Model").Type( HttpVerbs.Post).Data("sendAntiForgery"))
.Read(read => read.Action("AjaxServiceRead", "Model"))
.Update(x => x.Action("AjaxServiceUpdate", "Model").Type( HttpVerbs.Post).Data("sendAntiForgery"))
.Destroy(x => x.Action("AjaxServiceDelete", "Model").Type( HttpVerbs.Post).Data("sendAntiForgery"))
))
@section scripts {
<
script
type
=
"text/javascript"
>
function error_handler(e) {
if (e.errors) {
e.preventDefault(); // cancel grid rebind if error occurs
var message = "Errors:\n";
$.each(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);
}
}
function sendAntiForgery() {
return { "__RequestVerificationToken": $('input[name=__RequestVerificationToken]').val() };
}
</
script
>
}