views:

164

answers:

2

Hi,

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

+9  A: 

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.

User
+1  A: 

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" %>

jmservera