【ASP.NET MVC】【Razor】【VB】 Razor ~ ビュー・ヘルパー / サンプル編 [2] ~

はじめに

以下を扱う

 * ドロップダウンリスト(select/optionタグ)
 * ラジオボタン
 * リストボックス

サンプル

■ モデル

* PersonModel.vb
Imports System.Web.Mvc

Namespace Models
    Public Class PersonModel
        Private Property _Color As Color
        Private Property _ColorSelectListItems As IEnumerable(Of SelectListItem)
        Private Property _Gender As Gender
        Private Property _Friends As List(Of String)
        Private Property _FriendSelectListItems As IEnumerable(Of SelectListItem)

        Sub New()
            Me._ColorSelectListItems = New List(Of SelectListItem)
            Me._Friends = New List(Of String)
            Me._FriendSelectListItems = New List(Of SelectListItem)
        End Sub

        Public Property Color As Color
            Get
                Return Me._Color
            End Get
            Set(ByVal value As Color)
                Me._Color = value
            End Set
        End Property

        Public Property ColorSelectListItems As IEnumerable(Of SelectListItem)
            Get
                Return Me._ColorSelectListItems
            End Get
            Set(ByVal value As IEnumerable(Of SelectListItem))
                Me._ColorSelectListItems = value
            End Set
        End Property

        Public Property FriendSelectListItems As IEnumerable(Of SelectListItem)
            Get
                Return Me._FriendSelectListItems
            End Get
            Set(ByVal value As IEnumerable(Of SelectListItem))
                Me._FriendSelectListItems = 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

        Public Property Friends As List(Of String)
            Get
                Return Me._Friends
            End Get
            Set(ByVal value As List(Of String))
                Me._Friends = value
            End Set
        End Property
    End Class

    Public Enum Gender
        None
        Man
        Woman
    End Enum

    Public Enum Color
        None
        Red
        Blue
        Green
        Yellow
    End Enum
End Namespace

■ コントローラ

* PersonController.vb
Imports System.Web.Mvc
Imports WebAppli.Models

Namespace Controllers
    Public Class PersonController
        Inherits Controller

        ' GET: Person
        Function Index() As ActionResult
            Dim model As PersonModel = New PersonModel()
            model.ColorSelectListItems = New List(Of SelectListItem)(New SelectListItem() {
                New SelectListItem() With {.Value = Color.Red, .Text = Color.Red.ToString()},
                New SelectListItem() With {.Value = Color.Blue, .Text = Color.Blue.ToString()},
                New SelectListItem() With {.Value = Color.Yellow, .Text = Color.Yellow.ToString()}
            })
            model.FriendSelectListItems = New List(Of SelectListItem)(New SelectListItem() {
                New SelectListItem() With {.Value = "Mike", .Text = "Mike"},
                New SelectListItem() With {.Value = "Tom", .Text = "Tom"},
                New SelectListItem() With {.Value = "Smith", .Text = "Smith"}
            })
            Return View(model)
        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))

    ' ドロップダウンリスト

    ' 方法1
    @Html.EnumDropDownListFor(Function(model) model.Color)
    @<br />
    ' 方法2
    @Html.DropDownListFor(Function(model) model.Color, EnumHelper.GetSelectList((GetType(Models.Color))))
    @<br />
    ' 方法3
    @Html.DropDownListFor(Function(model) model.Color, Model.ColorSelectListItems)
    @<br />

    ' ラジオボタン

    @Html.RadioButtonFor(Function(model) model.Gender, Models.Gender.None,
                                         htmlAttributes:=New With {.checked = "checked"})
    @Html.Label("None")@<br />
    @Html.RadioButtonFor(Function(model) model.Gender, Models.Gender.Man)
    @Html.Label("Man")@<br />
    @Html.RadioButtonFor(Function(model) model.Gender, Models.Gender.Woman)
    @Html.Label("Woman")@<br />

    ' リストボックス

    @Html.ListBoxFor(Function(model) model.Friends, Model.FriendSelectListItems)

End Using


関連記事

Razor ~入門編~

http://blogs.yahoo.co.jp/dk521123/35586581.html

Razor ~ ビュー・ヘルパー / サンプル編 ~ [1]

http://blogs.yahoo.co.jp/dk521123/35592671.html

Razor ~ ビュー・ヘルパー / サンプル編 ~ [3]

http://blogs.yahoo.co.jp/dk521123/35609153.html

Razor ~ ビュー・ヘルパー / 構文編 ~

http://blogs.yahoo.co.jp/dk521123/35681489.html