Features

控えめな検証

控えめな検証

機能

説明

このサンプルは、控えめな検証の基本的な使用方法を示しています。

各列に対して、次の検証が行われます。


  • 名前: 必須。英数字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 Microsoft.AspNetCore.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{
   <script src="~/Scripts/jquery.js"></script>
    <script src="~/Scripts/jquery.validate.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
}

<c1-multi-row id="uvMultiRow" class="multirow" allow-add-new="true" allow-delete="true">
    <c1-items-source source-collection="Model"
                     create-action-url="@Url.Action("MultiRowCreateUserInfo")"
                     delete-action-url="@Url.Action("MultiRowDeleteUserInfo")"
                     update-action-url="@Url.Action("MultiRowUpdateUserInfo")">
    </c1-items-source>
    <c1-multi-row-cell-group>
        <c1-multi-row-cell binding="Id" is-read-only="true"></c1-multi-row-cell>
    </c1-multi-row-cell-group>
    <c1-multi-row-cell-group>
        <c1-multi-row-cell binding="Name"></c1-multi-row-cell>
        <c1-multi-row-cell binding="Industry"></c1-multi-row-cell>
    </c1-multi-row-cell-group>
    <c1-multi-row-cell-group colspan="3">
        <c1-multi-row-cell binding="Country"></c1-multi-row-cell>
        <c1-multi-row-cell binding="Phone" colspan="2"></c1-multi-row-cell>
        <c1-multi-row-cell binding="Email" colspan="2"></c1-multi-row-cell>
        <c1-multi-row-cell binding="Birthdate" format="M/d/yyyy"></c1-multi-row-cell>
    </c1-multi-row-cell-group>
</c1-multi-row>

@section Description{
<p>@Html.Raw(MultiRowRes.UnobtrusiveValidation_Text0)</p>

<p>@Html.Raw(MultiRowRes.UnobtrusiveValidation_Text1)</p>

}
using System;
using System.ComponentModel.DataAnnotations;

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]
        [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]
        [MinLength(8)]
        [MaxLength(11)]
        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 string FavoriteColor { get; set; }

        [Required]
        [MinLength(2)]
        [MaxLength(3)]
        public string[] Skills { get; set; }
    }
}