填充变量在模型中声明

时间:2013-06-13 19:39:20

标签: asp.net-mvc vb.net asp.net-mvc-4 razor

我目前正在使用Lynda.com tutorial在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变量应该填充新提供的信息。相反,它表明它没有填充,如下所示: properties within xauction isn't populating with new values for each property/variable

结果,现在我通过我的网络浏览器获得以下内容: output on the browser

我想知道:

  1. 为什么即使我有变量和所有声明的内容,它们也没有在模型中填充;和
  2. 如何通过View显示新填充的信息,而不必使用ViewData或任何传递实际对象的内容?
  3. 真正感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

在您的模型中,您所有属性的set方法实际上并没有保存任何内容。

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