FlexGrid
控えめな検証
このサンプルでは、控えめな検証の基本的な使用方法を示します。
機能
サンプル
説明
このサンプルでは、控えめな検証の基本的な使用方法を示します。
これらの列には、次の検証があります。
- Name:必須。英数字で 4 文字から 10 文字を含む。
- Industry:必須。
- Country:必須。
- Phone:必須。8 文字から 11 文字。
- Email:必須。有効な電子メールアドレス。
- Birthdate:必須。
ソース
UnobtrusiveValidationController.cs
using C1.Web.Mvc; using C1.Web.Mvc.Serialization; using Microsoft.AspNetCore.Mvc; using MvcExplorer.Models; using System; using System.Collections.Generic; using System.Linq; 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
@using MvcExplorer.Models @model List<UserInfo> @section Scripts{ <script src="~/Scripts/jquery.js"></script> <script src="~/Scripts/jquery.validate.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.js"></script> } <c1-flex-grid id="flexGrid" auto-generate-columns="false" allow-add-new="true" allow-delete="true" height="400px"> <c1-flex-grid-column binding="Id" is-read-only="true"></c1-flex-grid-column> <c1-flex-grid-column binding="Name"></c1-flex-grid-column> <c1-flex-grid-column binding="Email"></c1-flex-grid-column> <c1-flex-grid-column binding="Phone"></c1-flex-grid-column> <c1-flex-grid-column binding="Country"></c1-flex-grid-column> <c1-flex-grid-column binding="Industry"></c1-flex-grid-column> <c1-flex-grid-column binding="Birthdate" format="M/d/yyyy"></c1-flex-grid-column> <c1-items-source source-collection="Model" create-action-url="@Url.Action("GridCreateUserInfo")" delete-action-url="@Url.Action("GridDeleteUserInfo")" update-action-url="@Url.Action("GridUpdateUserInfo")"> </c1-items-source> </c1-flex-grid> @section Summary{ @Html.Raw(FlexGridRes.UnobtrusiveValidation_Text3) } @section Description{ <p>@Html.Raw(FlexGridRes.UnobtrusiveValidation_Text0)</p> <p>@Html.Raw(FlexGridRes.UnobtrusiveValidation_Text1)</p> }
UserInfo.cs
using System; using System.ComponentModel.DataAnnotations; namespace MvcExplorer.Models { public class UserInfo { public UserInfo() { Birthdate = DateTime.Now; } public int Id { get; set; } [Required(ErrorMessageResourceType = typeof(Localization.ValidationRes), ErrorMessageResourceName = "Required_Name_ErrorMessage")] [RegularExpression(pattern: "^[a-zA-Z0-9]{4,10}$", ErrorMessageResourceType =typeof(Localization.ValidationRes), ErrorMessageResourceName = "Register_Name_ErrorMessage")] public string Name { get; set; } [Required(ErrorMessageResourceType = typeof(Localization.ValidationRes), ErrorMessageResourceName = "Required_Email_ErrorMessage")] [EmailAddress] public string Email { get; set; } [Required] [MinLength(6)] [MaxLength(16)] public string Password { get; set; } [Required] [Compare("Password")] public string ConfirmPassword { get; set; } [Required(ErrorMessageResourceType = typeof(Localization.ValidationRes), ErrorMessageResourceName = "Required_Phone_ErrorMessage")] [MinLength(8)] [MaxLength(11)] public string Phone { get; set; } [Required(ErrorMessageResourceType = typeof(Localization.ValidationRes), ErrorMessageResourceName = "Required_Country_ErrorMessage")] public string Country { get; set; } [Required(ErrorMessageResourceType = typeof(Localization.ValidationRes), ErrorMessageResourceName = "Required_Industry_ErrorMessage")] public string Industry { get; set; } [Required(ErrorMessageResourceType = typeof(Localization.ValidationRes), ErrorMessageResourceName = "Required_Birthdate_ErrorMessage")] public DateTime Birthdate { get; set; } [Required] public string FavoriteColor { get; set; } [Required] [MinLength(2)] [MaxLength(3)] public string[] Skills { get; set; } [Required] public string[] Hobbies { get; set; } } }
マニュアル