Asp.net mvc 填充模型中声明的变量
我目前正在使用此工具在Visual Basic.NET中学习MVC 4和Razor 现在,我在Model文件夹中的一个名为Auction.vb的类中有我希望在整个网站中访问的主要变量,该类具有以下内容:Asp.net mvc 填充模型中声明的变量,asp.net-mvc,vb.net,asp.net-mvc-4,razor,Asp.net Mvc,Vb.net,Asp.net Mvc 4,Razor,我目前正在使用此工具在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
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
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
@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>
新控制器AuctionsController.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
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
@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>
正在将Auction()模型读入同名Auction.vbhtml的视图中。此视图具有以下功能:
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
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
@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>
@ModelType mvcaction.Models.Auction
@代码
尺寸测试=模型
结束代码
@遗嘱,头衔
开始时间:@testauction.StartTime.ToString()
结束时间:@testauction.EndTime.ToString()
起始价格:@testauction.StartPrice.ToString()
现行价格:
@*
由于CurrentPrice可为null,因此在调用.ToString()之前,我们必须检查它是否有值!
*@
@如果TestAction.CurrentPrice=无,则
@:[无投标]
其他的
@:@testauction.CurrentPrice.ToString()
如果结束
@如果TestAction.ImageURL不是String.Empty,则
@:
如果结束
@遗嘱说明
进入Visual Studio 2012,我在AuctionsController.vb类中设置了一个断点,xauction变量应该在该断点处填充新提供的信息。相反,它显示它没有填充任何内容,如下所示:
因此,现在我通过web浏览器获得以下信息:
我想知道:
非常感谢您的帮助。在您的模型中,您为所有属性设置的
方法实际上并没有保存任何内容
Public Property ID() As Long
Get
Return x_ID
End Get
Set(x_ID As Long)
End Set
End Property
应该是
Public Property ID() As Long
Get
Return x_ID
End Get
Set(value As Long)
x_ID = value
End Set
End Property
你的意思是说test_auction没有填充吗?在返回视图(test_auction)上放置一个断点代码>行并检查测试_
变量。它的值正确吗?@Guanxi我的意思是说test_拍卖对不起。谢谢谢谢谢谢@MattHouser!:)这就成功了。我真不敢相信事情竟如此简单。