MVC 3.0 problem rendering partial view from _layout.vbhtml, models are inaccessible

Discussion in 'ASP .Net' started by Mobileboy, Sep 1, 2011.

  1. Mobileboy

    Mobileboy Guest

    Hello group,

    I’m new to asp.NET MVC. I use VS 2010 and MVC 3.0 (Visual Basic .NET).
    I’m experimenting with partial views.
    As an exercise, I want to create a partial view that displays the current
    time.
    I want to add this partial view to the masterpage. In terms of Razor, I want
    to add the partial view to _Layout.html (below you find my questions).

    I have the following model:
    Namespace Models.ViewModels
    Public Class TimeViewData

    Public Function TimeAsstring() As String
    Return Now.ToString
    End Function


    End Class
    End Namespace


    I created a strongly typed view _Timeview (_Timeview.vbhtml) with the
    following code:

    @ModelType CreatePartialView.Models.ViewModels.TimeViewData

    @code
    Dim TestObject As string

    TestObject = Model.TimeAsstring
    End Code

    I want to display my parital view “_Timeview†on every page, so I added the
    following code to _Layout.vbhtml:

    <div id="TimeDesplay">
    @Html.Partial("_Timeview)
    </div>


    Here are my QUESTIONS:
    1. On the line “TestObject = Model.TimeAsstring†I always get a
    nullreference exception, it doesn’t contain the current time.
    What’s the problem?

    2. A null reference exception indicates that the model is not set to an
    object instance.
    So I tried to change the code in _layout.vbhtml. The BIG problem is that
    models.viewmodels.TimeviewData seems to be inaccessible from within
    _layout.vbhtml since I don’t get code completion and I get the following
    error:
    Type 'models.viewmodels.TimeviewData' is not defined.
    Why isn’t it accessible? What am I doing wrong?
    <div id="TimeDesplay">
    @code
    Dim MyTimeviewData As New models.viewmodels.TimeviewData

    Html.Partial("_Timeview", MyTimeviewData)
    End Code


    </div>
    Mobileboy, Sep 1, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    MVC Multiple Models

    Steven, May 2, 2005, in forum: Java
    Replies:
    1
    Views:
    2,632
    Jonck
    May 3, 2005
  2. vinjvinj
    Replies:
    15
    Views:
    570
    Jeremy Sanders
    Nov 10, 2005
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    430
    Jean-Paul Calderone
    Nov 7, 2005
  4. RichB
    Replies:
    7
    Views:
    5,704
    dkmorris1999
    Sep 21, 2009
  5. David Heinemeier Hansson
    Replies:
    0
    Views:
    232
    David Heinemeier Hansson
    Dec 23, 2004
Loading...

Share This Page