or
<
input
id
=
"SelectedRoles_Administrators"
type
=
"checkbox"
value
=
"Administrators"
name
=
"SelectedRoles"
data-bind
=
"checked:SelectedRoles"
>
<
label
for
=
"SelectedRoles_Administrators"
>Administratör</
label
>
<
input
id
=
"SelectedRoles_SuperAdministrators"
type
=
"checkbox"
value
=
"SuperAdministrators"
name
=
"SelectedRoles"
data-bind
=
"checked:SelectedRoles"
>
<
label
for
=
"SelectedRoles_SuperAdministrators"
>Superadministratör</
label
>
<
input
id
=
"SelectedRoles_Administrators"
type
=
"checkbox"
value
=
"Administrators"
name
=
"SelectedRoles"
>
<
label
for
=
"SelectedRoles_Administrators"
>Administratör</
label
>
<
input
id
=
"SelectedRoles_SuperAdministrators"
type
=
"checkbox"
value
=
"SuperAdministrators"
name
=
"SelectedRoles"
>
<
label
for
=
"SelectedRoles_SuperAdministrators"
>Superadministratör</
label
>
@foreach (var role in roles)
{
<
input
type
=
"checkbox"
name
=
"SelectedRoles"
id
=
"SelectedRoles_@(role.Enum)"
value
=
"@role.Enum"
/><
label
for
=
"SelectedRoles_@(role.Enum)"
>@role.Text</
label
>
}
public static IEnumerable<
SelectListItem
> SexListItems
{
get
{
foreach (var value in Enum.GetValues(typeof(Sex)))
{
string name = string.Format("Sex.{0}", Enum.GetName(typeof (Sex), value));
var text = Resources.Shared.EnumStrings.ResourceManager.GetString(name);
yield return new SelectListItem() {Value = value.ToString(), Text = text};
}
}
}
@Html.DropDownListFor(p => p.Sex, DataProviders.SexListItems)
@Html.Kendo().DropDownListFor(p => p.Sex).BindTo(ELSORegistry.DataProviders.SexListItems)
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(p => p.ID))
.Create(update => update.Action("EditingPopup_Create", "SysAdminController"))
.Read(read => read.Action("EditingPopup_Read", "SysAdminController"))
.Update(update => update.Action("EditingPopup_Update", "SysAdminController"))
.Destroy(update => update.Action("EditingPopup_Destroy", "SysAdminController"))
public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
{
throw new NotImplementedException();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingPopup_Create([DataSourceRequest] DataSourceRequest request, MenuTabViewViewModel menuTabViewViewModel)
{
throw new NotImplementedException();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingPopup_Update([DataSourceRequest] DataSourceRequest request, MenuTabViewViewModel product)
{
throw new NotImplementedException();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingPopup_Destroy([DataSourceRequest] DataSourceRequest request, MenuTabViewViewModel product)
{
throw new NotImplementedException();
}
That is rather strange, and my database, which does not allow entries with the same name fails the second (duplicate) entry. If I press F5 or next page on the grid after step 3 then I do not have this problem.
My .razor view is:
@{
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);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(172);
})
.ToolBar(toolbar => 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").Data("sendAntiForgery").Type( HttpVerbs.Post))
.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) {
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
>
}
The MVC actions are pretty standard copies of the format on your examples site, but with ValidateAntiForgeryToken added.
[AllowAnonymous]
public ActionResult Services(IListSmService service)
{
return View();
}
[AllowAnonymous]
public ActionResult AjaxServiceRead([DataSourceRequest]DataSourceRequest request, IListSmService service)
{
return Json(service.GetList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
[AllowAnonymous]
[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)
//errors, so copy the errors over to the ModelState
response.CopyErrorsToModelState(ModelState);
}
return Json(new[] { newItem }.ToDataSourceResult(request, ModelState));
}
[AllowAnonymous]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult AjaxServiceDelete([DataSourceRequest] DataSourceRequest request, ISmServiceDto itemToDelete, IDeleteSmService service)
{
if (itemToDelete != null)
{
var response = service.Delete(itemToDelete.SmServiceId);
if (!response.IsValid)
//errors, so copy the errors over to the ModelState
response.CopyErrorsToModelState(ModelState);
}
return Json(ModelState.ToDataSourceResult());
}
I must be doing wrong but I can't see it. Your help would be appreciated.