C#での開発経験がないため、C#のコードをVBに置き換えながらテキストを読み進めた。
VBのコードのメモを残す。
以下のサイトを参考にさせていただきました。
- LANCORK ASP.NET MVC 5実践プログラミングをC#からVB.NETに読み替える際にハマった点(基本編)
- ASP.NET MVC5 with VB『ASP.NET MVC5 実践プログラミング』をVBで
Chapter1 イントロダクション
概要や、環境構築の説明部分。
テキストではVisual Studio 2013 Expressを用いているが、こちらではVisual Studio 2015 Communityをインストールした。Chapter2 ASP.NET MVCの基本
※プロジェクトの参照設定の「インポートされた名前空間」にSystem.Data.Entityを追加しておくこと
リスト2-1 Controllers/BeginController.vb
Option Strict Off
Namespace Controllers
Public Class BeginController
Inherits Controller
' GET: Begin
Function Index() As ActionResult
Return Content("こんにちは、世界!")
End Function
End Class
End Namespace
リスト2-4 Controllers/BeginController.vb
Option Strict Off
Namespace Controllers
Public Class BeginController
Inherits Controller
...中略...
Function Show() As ActionResult
ViewBag.Message = "こんにちは、世界!"
Return View()
End Function
End Class
End Namespace
リスト2-5 Views/Begin/Show.vbhtml
@Code
ViewData("Title") = "Show"
End Code
<h2>Show</h2>
<p>@ViewBag.Message</p>
リスト2-7 Models/Member.vb
Public Class Member
Public Property Id As Integer
Public Property Name As String
Public Property Email As String
Public Property Birth As DateTime
Public Property Married As Boolean
Public Property Memo As String
End Class
リスト2-8 Models/MvcBasicContext.vb
Public Class MvcBasicContext
Inherits DbContext
Public Property Members As DbSet(Of Member)
End Class
リスト2-9 Web.config
<connectionStrings>
...中略...
<add name="MvcBasicContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\MvcBasic.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
リスト2-10 Models/MvcBasicInitializer.vb
Public Class MvcBasicInitializer
Inherits DropCreateDatabaseAlways(Of MvcBasicContext)
Protected Overrides Sub Seed(context As MvcBasicContext)
Dim members As New List(Of Member) _
From {
New Member() With {
.Name = "山田太郎",
.Email = "yamada@example.com",
.Birth = DateTime.Parse("1970-04-01"),
.Married = False,
.Memo = "メモ欄です。"
},
New Member() With {
.Name = "佐藤花子",
.Email = "sato@example.com",
.Birth = DateTime.Parse("1972-12-12"),
.Married = False,
.Memo = "備考欄です。"
}
}
members.ForEach(Function(b) context.Members.Add(b))
context.SaveChanges()
End Sub
End Class
リスト2-11 Global.asax
Imports System.Web.Optimization
Public Class MvcApplication
Inherits System.Web.HttpApplication
Protected Sub Application_Start()
...中略...
Database.SetInitializer(Of MvcBasicContext)(New MvcBasicInitializer())
End Sub
End Class
リスト2-12 Controllers/BeginController.vb
Option Strict Off
Namespace Controllers
Public Class BeginController
Inherits Controller
Private db As New MvcBasicContext
...中略...
Function List() As ActionResult
Return View(db.Members)
End Function
End Class
End Namespace
リスト2-13 Views/Begin/List.vbhtml
@ModelType IEnumerable(Of MvcBasic.Member)
@Code
ViewData("Title") = "List"
End Code
<h2>List</h2>
<table class="table">
<tr>
<th>氏名</th>
<th>メールアドレス</th>
<th>誕生日</th>
<th>既婚</th>
<th>備考</th>
</tr>
@For Each item In Model
@<tr>
<td>@item.Name</td>
<td>@item.Email</td>
<td>@item.Birth</td>
<td>@item.Married</td>
<td>@item.Memo</td>
</tr>
Next
</table>
リスト2-14 Controllers/BeginController.vb
Option Strict Off
Namespace Controllers
Public Class BeginController
Inherits Controller
...中略...
Sub New()
db.Database.Log = Sub(sql) Debug.Write(sql)
End Sub
...中略...
End Class
End Namespace
Chapter3 Scaffolding機能による開発
リスト3-3 Models/Member.vb
Imports System.ComponentModel
Public Class Member
Public Property Id As Integer
<DisplayName("名前")>
Public Property Name As String
<DisplayName("メールアドレス")>
Public Property Email As String
<DisplayName("生年月日")>
Public Property Birth As DateTime
<DisplayName("既婚")>
Public Property Married As Boolean
<DisplayName("自己紹介")>
Public Property Memo As String
End Class
0 件のコメント:
コメントを投稿