FlexGrid
Unobtrusive Validation
This sample shows the basic usage of the unobtrusive validation.
Features
Sample
Description
This sample shows the basic usage of the unobtrusive validation.
The columns have the following validations:
- Name: Required. Alphanumeric and contains 4 to 10 characters.
- Industry: Required.
- Country: Required.
- Phone: Required. 8 to 11 characters.
- Email: Required. Valid e-mail address.
- Birthdate: Required.
Source
UnobtrusiveValidationController.cs
using C1.Web.Mvc; using C1.Web.Mvc.Serialization; using MvcExplorer.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; namespace MvcExplorer.Controllers { public partial class FlexGridController : Controller { private static List<UserInfo> users = UsersData.Users; public ActionResult UnobtrusiveValidation() { return View(users); } public ActionResult GridUpdateUserInfo([C1JsonRequest]CollectionViewEditRequest<UserInfo> requestData) { return this.C1Json(CollectionViewHelper.Edit<UserInfo>(requestData, item => { string error = string.Empty; bool success = true; try { var resultItem = users.Find(u => u.Id == item.Id); var index = users.IndexOf(resultItem); users.Remove(resultItem); users.Insert(index, item); } catch (Exception e) { error = e.Message; success = false; } return new CollectionViewItemResult<UserInfo> { Error = error, Success = success, Data = item }; }, () => users)); } public ActionResult GridCreateUserInfo([C1JsonRequest]CollectionViewEditRequest<UserInfo> requestData) { return this.C1Json(CollectionViewHelper.Edit<UserInfo>(requestData, item => { string error = string.Empty; bool success = true; try { users.Add(item); item.Id = users.Max(u => u.Id) + 1; } catch (Exception e) { error = e.Message; success = false; } return new CollectionViewItemResult<UserInfo> { Error = error, Success = success, Data = item }; }, () => users)); } public ActionResult GridDeleteUserInfo([C1JsonRequest]CollectionViewEditRequest<UserInfo> requestData) { return this.C1Json(CollectionViewHelper.Edit<UserInfo>(requestData, item => { string error = string.Empty; bool success = true; try { var resultItem = users.Find(u => u.Id == item.Id); users.Remove(resultItem); } catch (Exception e) { error = e.Message; success = false; } return new CollectionViewItemResult<UserInfo> { Error = error, Success = success, Data = item }; }, () => users)); } } }
UnobtrusiveValidation.cshtml
@model List<UserInfo> @section Scripts{ @Scripts.Render("~/jquery") @Scripts.Render("~/jqueryval") } @(Html.C1().FlexGrid<UserInfo>() .Id("flexGrid") .AutoGenerateColumns(false) .Columns(columns => columns .Add(c => c.Binding("Id").IsReadOnly(true)) .Add(c => c.Binding("Name")) .Add(c => c.Binding("Email")) .Add(c => c.Binding("Phone")) .Add(c => c.Binding("Country")) .Add(c => c.Binding("Industry")) .Add(c => c.Binding("Birthdate").Format("M/d/yyyy")) ) .Bind(ib => ib.Bind(Model) .Update(Url.Action("GridUpdateUserInfo")) .Create(Url.Action("GridCreateUserInfo")) .Delete(Url.Action("GridDeleteUserInfo")) ) .AllowAddNew(true) .AllowDelete(true) .CssStyle("height", "400px") ) @section Summary{ @Html.Raw(Resources.FlexGrid.UnobtrusiveValidation_Text2) } @section Description{ <p>@Html.Raw(Resources.FlexGrid.UnobtrusiveValidation_Text0)</p> <p>@Html.Raw(Resources.FlexGrid.UnobtrusiveValidation_Text1)</p> <ul> <li>@Html.Raw(Resources.FlexGrid.UnobtrusiveValidation_Li1)</li> <li>@Html.Raw(Resources.FlexGrid.UnobtrusiveValidation_Li2)</li> <li>@Html.Raw(Resources.FlexGrid.UnobtrusiveValidation_Li3)</li> <li>@Html.Raw(Resources.FlexGrid.UnobtrusiveValidation_Li4)</li> <li>@Html.Raw(Resources.FlexGrid.UnobtrusiveValidation_Li5)</li> <li>@Html.Raw(Resources.FlexGrid.UnobtrusiveValidation_Li6)</li> </ul> }
UserInfo.cs
using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Drawing; namespace MvcExplorer.Models { public class UserInfo { public UserInfo() { Birthdate = DateTime.Now; } public int Id { get; set; } [Required] [RegularExpression(pattern: "^[a-zA-Z0-9]{4,10}$", ErrorMessageResourceType = typeof(Resources.Validation), ErrorMessageResourceName = "Register_Name_ErrorMessage")] public string Name { get; set; } [Required] [RegularExpression(pattern: @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessageResourceType = typeof(Resources.Validation), ErrorMessageResourceName = "Register_Email_ErrorMessage")] public string Email { get; set; } [Required] [StringLength(16, MinimumLength = 6)] public string Password { get; set; } [Required] [Compare("Password")] public string ConfirmPassword { get; set; } [Required] [StringLength(11, MinimumLength = 8)] public string Phone { get; set; } [Required] public string Country { get; set; } [Required] public string Industry { get; set; } [Required] public DateTime Birthdate { get; set; } [Required] public Color? FavoriteColor { get; set; } [Required] public string[] Skills { get; set; } [Required] public string[] Hobbies { get; set; } } }
Documentation