注意 / 補足
@Html.LabelFor
* 「.」(ピリオド)があるとその前の文字が表示されなくなる 【例】 @Html.Label("Hello.World!") => 「World!」と表示される
@Html.XxxxFor / XxxxForModel
* 「XxxxForModel」だとラムダ式が省略できる 【例】 @Html.LabelFor(Function(model) model.Id) @Html.LabelForModel(model.Id) ' ラムダ式が省略できる
サンプル
■ モデル
* PersonModel.vbNamespace Models Public Class PersonModel Private Property _Id As Long Private Property _Name As String Public Property Id As Long Get Return Me._Id End Get Set(ByVal value As Long) Me._Id = value End Set End Property Public Property Name As String Get Return Me._Name End Get Set(ByVal value As String) Me._Name = value End Set End Property 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 Return View(model) 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.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.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"}}) @<input type = "submit" value="Send" Class="btn btn-default" /> End Using* Result.vbhtml
@ModelType WebAppli.Models.PersonModel @Code ViewData("Title") = "Edit" End Code <h2>Result</h2> @Html.DisplayFor(Function(model) model.Id)<br /> <br /> @Html.EditorFor(Function(model) model.Name)<br /> <br />
参考文献
VB/C#http://www.atmarkit.co.jp/fdotnet/aspnetmvc3/aspnetmvc3_06/aspnetmvc3_06_03.html
C#
http://office-yone.com/asp-net-mvc-htmlhelper/
http://blog.shibayan.jp/entry/20110327/1301152413