概要
ASP.net MVC5 実践プログラミングの書籍を勉強してて、VBで記述する場合どうやるの?
Oracleへの接続はどうやるの?って疑問に思って。忘れないように。
環境
Windows7 SP1 32bit
Oracle11g
Visual Studio2013 Professional
IE11
前提
データベース上にテーブルが作成されていること
Members
CREATE TABLE MEMBERS
(
"ID" NUMBER NOT NULL,
"NAME" VARCHAR2(30 BYTE),
"EMAIL" VARCHAR2(80 BYTE),
"BIRTH" DATE,
"MARRIED" CHAR(1 BYTE),
"MEMO" VARCHAR2(120 BYTE)
)
Insertは省く
手順
※基本的には本に従って進める(Chapter2)
1.プロジェクトを作成 + ビルド プロジェクト名:WebApplication7
2.Modelsフォルダに 新規EDM(ASP.NET Entity Data Model) を追加 + ビルド
モデル名:MembersModel Entities名:ModelEntities
(データベースから EF Designer)
3.Controllesフォルダに コントロール追加 + ビルド コントローラ名:BeginController
(MVC5 コントローラ名(空))
4.BeginController.vbに以下を追記
Public Class BeginController
Inherits Controller
Private db As New ModelEntities ’この行
Function Index() As ActionResult
Return View(db.MEMBERS.ToList()) ’この行
End Function
5.Index()メソッドを右クリックして ビューの追加 + ビルド ビュー名:Index 他デフォルト
6.Index.vbhtmlに以下を追記
@ModelType IEnumerable(Of MEMBERS)
@Code
ViewData("Title") = "Index"
End Code
<h2>Index</h2>
以下、全部追記
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(Function(model) model.NAME)
</th>
<th>
@Html.DisplayNameFor(Function(model) model.EMAIL)
</th>
<th>
@Html.DisplayNameFor(Function(model) model.BIRTH)
</th>
<th>
@Html.DisplayNameFor(Function(model) model.MARRIED)
</th>
<th>
@Html.DisplayNameFor(Function(model) model.MEMO)
</th>
</tr>
@For Each item In Model
@<tr>
<td>
@Html.DisplayFor(Function(modelItem) item.NAME)
</td>
<td>
@Html.DisplayFor(Function(modelItem) item.EMAIL)
</td>
<td>
@Html.DisplayFor(Function(modelItem) item.BIRTH)
</td>
<td>
@Html.DisplayFor(Function(modelItem) item.MARRIED)
</td>
<td>
@Html.DisplayFor(Function(modelItem) item.MEMO)
</td>
</tr>
Next
</table>
7.実行
9.最後に
Scaffolding機能で 追加・編集・削除もできる。この本は 僕にとって とても役にたつ!
10.履歴
2016/10/07 コード部分をMarkdown記法に合わせて修正。