mrblah
mrblah

Reputation: 103627

In asp.net mvc, where do I put my strongly typed viewdata reference in my viewpage?

My viewpage doesn't have a codebehind, so how do I tell it to use a strongly typed viewdata?

Upvotes: 1

Views: 266

Answers (2)

jmservera
jmservera

Reputation: 6682

You can of course create a strongly typed viewdata by inheriting from it and adding a .cs file like this:

  1. Create a .cs file (i.e.: if you have "Index.aspx" call it "Index.cs") next to your view.
  2. Create a class that inherits from the System.Web.Mvc.ViewPage class
  3. Modify the aspx file to inherit from it:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="MyNamespace.MyViewPage" %>

Upvotes: 1

User
User

Reputation: 30985

Just in header:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
            Inherits="System.Web.Mvc.ViewPage<HomePageViewModel>" %>

Then you can access your strongly typed model like this:

<%= Model.Username %>

"Model" property is automatically cast to your type.

Upvotes: 8

Related Questions