FlexGrid
Unobtrusive Validation
This sample shows the basic usage of the unobtrusive validation.
Features
Id
Name
Email
Phone
Country
Industry
Birthdate
1
John
John@gmail.com
1424685445
Albania
Computers
1/1/2001
2
Mary
Mary@gmail.com
1296479754
American
Electronics
3/2/1985
3
David
David@gmail.com
1217654653
Australia
Telecom
3/1/1999
4
Sunny
Sunny@gmail.com
1756456786
Bosnia
Internet
4/3/1989
5
James
James@gmail.com
1209687543
Botswana
Accounting
3/2/1994
6
Maria
Maria@gmail.com
1543578643
Bahrain
Accounting
4/2/1998
7
Michael
Michael@gmail.com
1215457467
Argentina
Finance
2/2/2003
8
Michelle
Michelle@gmail.com
1534357546
Bulgaria
Finance
1/1/2001
Id
Name
Email
Phone
Country
Industry
Birthdate
0
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 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)); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | @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 > } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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 ; } } } |