■ 基本知識
ControllerクラスのModelState
* モデルのエラー情報を管理 【代表的なAPI】 * ModelState.AddModelError() : エラー情報の追加 * ModelState.IsValid() : 検証時にエラーが見つかった場合に false を返す * ModelState.Clear() : 受け取ったデータをクリアする => ModelState.Clear() しないことにより、問題がおこった例は以下のサイトを参照のこと。http://okwave.jp/qa/q7744657.html
https://social.msdn.microsoft.com/Forums/ja-JP/052bdb45-17b3-4478-af38-eebb5c3f63b7/aspnet-mvc-3-htmldisplayfor-htmlhiddenfor-?forum=aspnetja
■ サンプル
モデル
* PersonModel.vbImports System.Web.Mvc Namespace Models Public Class PersonModel Private Property _Id As Long Private Property _Name As String Private Property _Gender As Gender <ComponentModel.DisplayName("ID")> <ComponentModel.DataAnnotations.Required(ErrorMessage:="{0}は必須です。")> <ComponentModel.DataAnnotations.Range(100, 10000, ErrorMessage:="{0}は{1}~{2}の間で入力してください。")> Public Property Id As Long Get Return Me._Id End Get Set(ByVal value As Long) Me._Id = value End Set End Property <ComponentModel.DisplayName("名前")> <ComponentModel.DataAnnotations.Required(ErrorMessage:="{0}は必須です。")> <ComponentModel.DataAnnotations.StringLength(100, ErrorMessage:="{0}は{1}文字以内で入力してください。")> Public Property Name As String Get Return Me._Name End Get Set(ByVal value As String) Me._Name = value End Set End Property Public Property Gender As Gender Get Return Me._Gender End Get Set(ByVal value As Gender) Me._Gender = value End Set End Property End Class Public Enum Gender None Man Woman End Enum End Namespace
コントローラ
* PersonController.vbImports System.Web.Mvc Imports WebAppli.Models Namespace Controllers Public Class PersonController Inherits Controller ' GET: Person Function Index() As ActionResult Return View() End Function ' GET: Person Function Result(model As PersonModel) As ActionResult If ModelState.IsValid Then Return View(model) Else Return View("Index") End If End Function End Class End Namespace
ビュー
* Index.vbhtml@ModelType WebAppli.Models.PersonModel @Code ViewData("Title") = "View" End Code <h2>View</h2> @Using (Html.BeginForm( "Result", ' アクション名 "Person", ' コントローラ名 FormMethod.Post)) @Html.AntiForgeryToken() @Html.ValidationSummary(False, "", New With {.class = "text-danger"}) @Html.LabelFor(Function(model) model.Id, htmlAttributes:=New With {.class = "control-label col-md-2"}) @Html.EditorFor(Function(model) model.Id, New With {.htmlAttributes = New With {.class = "form-control"}}) @Html.ValidationMessageFor(Function(model) model.Id, "*") @<br /> @Html.LabelFor(Function(model) model.Name, htmlAttributes:=New With {.class = "control-label col-md-2"}) @Html.EditorFor(Function(model) model.Name, New With {.htmlAttributes = New With {.class = "form-control"}}) @Html.ValidationMessageFor(Function(model) model.Name, "*") @<br /> @Html.EnumDropDownListFor(Function(model) model.Gender, "Please select", Nothing) @Html.ValidationMessageFor(Function(model) model.Gender, "*") @<br /> @<input type = "submit" value="Send" Class="btn btn-default" /> End Using* Result.vbhtml
@ModelType WebAppli.Models.PersonModel @Code ViewData("Title") = "Result" End Code <h2>Result</h2> @Html.DisplayFor(Function(model) model.Id)<br /> <br /> @Html.DisplayFor(Function(model) model.Name)<br /> <br /> @Html.DisplayFor(Function(model) model.Gender)<br /> <br />
参考文献
* C#/VBhttp://www.atmarkit.co.jp/fdotnet/aspnetmvc3/aspnetmvc3_04/aspnetmvc3_04_02.html
http://www.atmarkit.co.jp/fdotnet/aspnetmvc3/aspnetmvc3_04/aspnetmvc3_04_03.html
* C#
https://code.msdn.microsoft.com/ASPNet-MVC-5f2b021b