Features
控えめな検証
控えめな検証
機能
説明
このサンプルは、MultiRowコントロールで控えめな検証を使用する方法を示します。
各列に対して、次の検証が行われます。
- 名前: 必須。英数字4~10文字。
- 業種: 必須。
- 国: 必須。
- 電話: 必須。8~11文字。
- 電子メール: 必須。有効な電子メールアドレス。
- 生年月日: 必須。
using C1.Web.Mvc; using C1.Web.Mvc.Serialization; using MultiRowExplorer.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; namespace MultiRowExplorer.Controllers { public partial class MultiRowController : Controller { private static List<UserInfo> users = UsersData.Users; public ActionResult UnobtrusiveValidation() { return View(users); } public ActionResult MultiRowUpdateUserInfo([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 MultiRowCreateUserInfo([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 MultiRowDeleteUserInfo([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)); } } }
@model List<UserInfo> @section Scripts{ @Scripts.Render("~/jquery") @Scripts.Render("~/jqueryval") } @(Html.C1().MultiRow<UserInfo>() .Id("uvMultiRow") .LayoutDefinition(ld => { ld.Add().Colspan(1).Cells(cells => { cells.Add(c => c.Binding("Id").IsReadOnly(true)); }); ld.Add().Colspan(1).Cells(cells => { cells.Add(c => c.Binding("Name")) .Add(c => c.Binding("Industry")); }); ld.Add().Colspan(3).Cells(cells => { cells.Add(c => c.Binding("Country")) .Add(c => c.Binding("Phone").Colspan(2)) .Add(c => c.Binding("Email").Colspan(2)) .Add(c => c.Binding("Birthdate").Format("M/d/yyyy")); }); }) .Bind(ib => ib.Bind(Model) .Update(Url.Action("MultiRowUpdateUserInfo")) .Create(Url.Action("MultiRowCreateUserInfo")) .Delete(Url.Action("MultiRowDeleteUserInfo")) ) .AllowAddNew(true) .AllowDelete(true) .CssClass("multirow") ) @section Description{ <p>@Html.Raw(Resources.MultiRowExplorer.UnobtrusiveValidation_Text0)</p> <p>@Html.Raw(Resources.MultiRowExplorer.UnobtrusiveValidation_Text1)</p> }
using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Drawing; namespace MultiRowExplorer.Models { public class UserInfo { public UserInfo() { Birthdate = DateTime.Now; } public int Id { get; set; } [Required] [RegularExpression(pattern: "^[a-zA-Z0-9]{4,10}$", ErrorMessage = "The username must be alphanumeric and contains 4 to 10 characters.")] public string Name { get; set; } [Required] [RegularExpression(pattern: @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "The Email field is not a valid e-mail address.")] 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; } } }