UsersManagement.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace RDVMPortalDAL.Models
{
public class UsersManagement
{
[Required(ErrorMessage = "Please enter First Name")]
[RegularExpression("^[a-zA-Z]+$", ErrorMessage = "Please Enter only Alphabets for First Name")]
public string FirstName { get; set; }
}
}
UserManagement.cshtml
@(Html.Kendo().Grid<RDVMPortalDAL.Models.UsersManagement>()
.Name("UsersInfoGrid")
.Columns(columns =>
{
columns.Bound(c => c.FirstName);
columns.Bound(c => c.LastName);
columns.Bound(c => c.Address).ClientTemplate("#:Address#, #:City#, #:State# - #:Zip#");
columns.Bound(c => c.PhoneNumber);
columns.Bound(c => c.Email);
columns.Bound(c => c.UserName);
columns.Bound(c => c.Role);
columns.Bound(c => c.RegisteredBy);
columns.Command(commands => { commands.Edit(); });
})
.Sortable()
.Pageable()
.Groupable()
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("UsersDetailEditTemplate").Window(window=>window.Title("Edit User Information")))
.DataSource(dataSource => dataSource
.Ajax()
.Events(e => e.Sync("handlers"))
.Model(mods =>
{
mods.Id(c => c.UserId);
mods.Field(c => c.UserId).Editable(false);
})
.Read(read=>read.Action("ReadUsersInfoGrid", "Home"))
.Update(update=>update.Action("UpdateUsersInfoGrid","Home"))
.PageSize(20)
)
)
UsersDetailEditTemplate.cshtml
@model RDVMPortalDAL.Models.UsersManagement
@Html.HiddenFor(u => u.UserId)
<div class="row">
@Html.Label("First Name : ", new { @class = "control-label col-sm-4 lbl" })
<div class="col-sm-5 divs">
@Html.TextBoxFor(u => u.FirstName, new { @class = "form-control inputcntrl" })
@Html.ValidationMessageFor(u => u.FirstName, "", new { @class = "text-danger" })
</div>
</div>
控制器
[HttpPost]
public ActionResult UpdateUsersInfoGrid([DataSourceRequest]DataSourceRequest request, UsersManagement usersManagement
{
if (ModelState.IsValid)
{
sqldal.UpdateUsersInfoData(usersManagement);
}
return Json(new[] { usersManagement }.ToDataSourceResult(request, ModelState));
}
因此,当我从网格进行编辑时,除了验证之外所有东西都能正常工作,所以如果我删除名称sill它需要显示ErrorMessage的旧数据并进行更新,当我调试代码时Modelstate.Isvalid条件工作正常但仍未获得为什么错误消息不会出现。
任何帮助,请谢谢。