• 検索結果がありません。

– Address コントローラーの新しいビューを作成する

図 26

タスク 9 – Address コントローラーの新しいビューを作成する

</p>

<p>

<label for="PostalCode">Postal Code:</label>

<%=Html.TextBox("Address.PostalCode")%>

</p>

<p>

<label for="CountryRegion">Country/Region:</label>

<%=Html.TextBox("Address.CountryRegion")%>

</p>

<p>

<input type="submit" value="Save" />

</p>

</fieldset>

<% End Using%>

</asp:Content>

メモ

: IDisposable

パターンと

using

キーワードを使用して

Form

の宣言を自動終了す るフォームをレンダリングするために、

Html.BeginForm

ヘルパーメソッドをどの ように使用しているかに注目してください。フォーム送信メソッド呼び出しが、適

切な

URL

形式に変換されます。

メモ

:

上記で使用した

Html.TextBox

ヘルパーメソッドは、テキストボックスの

HTML

コードを動的に生成します。引数として、

HTML

名、表示するテキスト、お

よび目的の要素の

HTML

属性を含む匿名オブジェクトを受け取ります。

テキストボックスの値は指定しない場合もあります。これは、

Html.TextBox

ヘル パーメソッドが、テキストボックスに指定したのと同じ名前の、

ViewData (

または

ViewData.Model)

のプロパテゖを探すためです。同じ名前のプロパテゖが見つかれ

ば、その値を取得して使用します。

39

Create

ビューの

[Add View] (

ビューの追加

) (C#)

40

Create

ビューの

[Add View] (

ビューの追加

) (Visual Basic)

2. [Add View] (

ビューの追加

)

ダ゗ゕログボックスで次の値を指定し、

[Add] (

追加

)

クリックしてビューを作成します。

[View Name] (

ビュー名

): Create

[Create a strongly-typed view] (

厳密に型指定されたビューを作成する

)

チェック

ボックス

:

オン

[View data class] (

ビューデータクラス

):

MvcSampleApp.ViewData.AddressViewData (C#)

または

MvcSampleApp. AddressViewData (Visual Basic)

[View content] (

ビューのコンテンツ

): Create (

作成

)

[Select master page] (

マスターページを選択する

)

チェックボックス

:

オン

 既定のマスターページの値

: ~/Views/Shared/Site.Master

[ContentPlaceHolder ID]: MainContent

41

Create

ビューの追加

(C#)

42

Create

ビューの追加

(Visual Basic)

3.

ビューに空のフォームを実装して、新しい住所のユーザー入力を受け取ります。

これを行うには、ソリューションエクスプローラーで、

(Views\Address

フォルダ

ーにある

) Create.aspx

をダブルクリックして、フゔ゗ルを開きます。

2

つ目の

<asp:Content>

タグを次のコードに置き換えます。

ASP.NET (C#)

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>

Create Address </h2>

<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>

<% using (Html.BeginForm()) {%>

<fieldset>

<legend>Fields</legend>

<p>

<label for="AddressLine1">

Address Line 1:</label>

<p>

<label for="AddressLine2">

Address Line 2:</label>

<%= Html.TextBox("Address.AddressLine2") %>

</p>

<p>

<label for="City">

City:</label>

<%= Html.TextBox("Address.City")%>

</p>

<p>

<label for="StateProvince">

State/Province:</label>

<%= Html.TextBox("Address.StateProvince")%>

</p>

<p>

<label for="PostalCode">

Postal Code:</label>

<%= Html.TextBox("Address.PostalCode")%>

</p>

<p>

<label for="CountryRegion">

Country/Region:</label>

<%= Html.TextBox("Address.CountryRegion")%>

</p>

<p>

<input type="submit" value="Create" />

</p>

</fieldset>

<% } %>

</asp:Content>

ASP.NET (Visual Basic)

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>

Create Address </h2>

<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>

<% Using Html.BeginForm()%>

<fieldset>

<legend>Fields</legend>

<p>

<label for="AddressLine1">

Address Line 1:</label>

<%= Html.TextBox("Address.AddressLine1") %>

</p>

<p>

<label for="AddressLine2">

Address Line 2:</label>

<%= Html.TextBox("Address.AddressLine2") %>

<%= Html.TextBox("Address.City")%>

</p>

<p>

<label for="StateProvince">

State/Province:</label>

<%= Html.TextBox("Address.StateProvince")%>

</p>

<p>

<label for="PostalCode">

Postal Code:</label>

<%= Html.TextBox("Address.PostalCode")%>

</p>

<p>

<label for="CountryRegion">

Country/Region:</label>

<%= Html.TextBox("Address.CountryRegion")%>

</p>

<p>

<input type="submit" value="Create" />

</p>

</fieldset>

<% End Using%>

</asp:Content>

次の手順

関連したドキュメント