図 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>