My viewpage doesn't have a codebehind, so how do I tell it to use a strongly typed viewdata?
Asked
Active
Viewed 262 times
2 Answers
8
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
- 30,403
- 22
- 79
- 107
1
You can of course create a strongly typed viewdata by inheriting from it and adding a .cs file like this:
- Create a .cs file (i.e.: if you have "Index.aspx" call it "Index.cs") next to your view.
- Create a class that inherits from the System.Web.Mvc.ViewPage class
Modify the aspx file to inherit from it:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="MyNamespace.MyViewPage" %>

jmservera
- 6,454
- 2
- 32
- 45