我目前正在使用这个Lynda.com 教程在 Visual Basic.NET 中学习 MVC 4 和 Razor 。
现在,我在 Model 文件夹中名为 Auction.vb 的类中拥有想要在整个网站上访问的主要变量,该类具有以下内容:
Namespace Models
Public Class Auction
Private Property x_ID As Long
Private Property x_Title As String
Private Property x_Description As String
Private Property x_ImageURL As String
Private Property x_StartTime As DateTime
Private Property x_EndTime As DateTime
Private Property x_StartPrice As Decimal
Private Property x_CurrentPrice As Decimal
Public Property ID() As Long
Get
Return x_ID
End Get
Set(x_ID As Long)
End Set
End Property
Public Property Title() As String
Get
Return x_Title
End Get
Set(value As String)
End Set
End Property
Public Property Description() As String
Get
Return x_Description
End Get
Set(x_Description As String)
End Set
End Property
Public Property ImageURL() As String
Get
Return x_ImageURL
End Get
Set(x_ImageURL As String)
End Set
End Property
Public Property StartTime() As DateTime
Get
Return x_StartTime
End Get
Set(x_StartTime As DateTime)
End Set
End Property
Public Property EndTime() As DateTime
Get
Return x_EndTime
End Get
Set(x_EndTime As DateTime)
End Set
End Property
Public Property StartPrice() As Decimal
Get
Return x_StartPrice
End Get
Set(x_StartPrice As Decimal)
End Set
End Property
Public Property CurrentPrice() As Decimal
Get
Return x_CurrentPrice
End Get
Set(x_CurrentPrice As Decimal)
End Set
End Property
End Class
End Namespace
这是由新控制器 AuctionsController.vb 读取的。该控制器具有以下功能:
Namespace Models
Public Class AuctionsController
Inherits System.Web.Mvc.Controller
'
' GET: /Auctions
Function Index() As ActionResult
Return View()
End Function
Public Function Auction() As ActionResult
Dim test_auction = New MVCAuction.Models.Auction() With { _
.Title = "Example Auction", _
.Description = "This is an example Auction", _
.StartTime = DateTime.Now, _
.EndTime = DateTime.Now.AddDays(7), _
.StartPrice = 1D, _
.CurrentPrice = Nothing _
}
Return View(test_auction)
End Function
End Class
End Namespace
Auction() 模型被读入同名视图 Auction.vbhtml。此视图具有以下内容:
@ModelType MVCAuction.Models.Auction
@Code
Dim testauction = Model
End Code
<div class="auction">
<h3>@testauction.Title</h3>
<div class="details">
<p>Start time: @testauction.StartTime.ToString()</p>
<p>End time: @testauction.EndTime.ToString()</p>
<p>Starting price: @testauction.StartPrice.ToString()</p>
<p>Current price:
@*
Since CurrentPrice is nullable we have to check to see if it has a value before we call .ToString()!
*@
@If testauction.CurrentPrice = Nothing Then
@: [No bids]
Else
@: <span>@testauction.CurrentPrice.ToString()</span>
End If
</p>
</div>
@If testauction.ImageURL IsNot String.Empty Then
@: <img src="@testauction.ImageURL" title="@testauction.Title" />
End If
<div class="description">
@testauction.Description
</div>
</div>
进入 Visual Studio 2012,我在 AuctionsController.vb 类中设置了一个断点,xauction 变量应该在该类中填充新提供的信息。相反,它表明它没有填充任何内容,如下所示:
结果,现在我通过我的网络浏览器得到以下信息:
我想知道:
- 为什么即使我声明了变量和所有内容,它们也没有填充到模型中;和
- 我怎样才能让新填充的信息通过视图显示,而不必使用 ViewData 或任何传递实际对象的东西?
任何帮助都非常感谢。