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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.