birdus
birdus

Reputation: 7504

How to access strongly typed data in MVC

I've been searching for a good explanation of how to access my strongly typed data from the View of my MVC app (first time touching MVC) and can't seem to find it. Here's the code in my controller:

PersonDetailsModel personDetails = personProvider.GetPersonDetails(id);
return View("Person", personDetails);

I have a view called Person.aspx which looks like this (pretty much empty):

<%@ Page Title="Title" Language="C#" Inherits="System.Web.Mvc.ViewPage<Models.PersonDetailsModel>" MasterPageFile="../MvcMasterPage.Master" %>

I would've thought I could just do something like Model.property or Person.property in the view to access the data, but I don't see how I can access the instance of my model. I'm sure it's easy, but I just don't see it.

Upvotes: 0

Views: 211

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

Since you have a strongly typed view, Model is the instance of your model that you passed from the controller. So you could directly access its properties:

<%@ Page 
    Title="Title" 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewPage<Models.PersonDetailsModel>"
    MasterPageFile="../MvcMasterPage.Master" 
%>

<div><%: Model.SomeProperty %></div>

The Model property will be of type Models.PersonDetailsModel.

And if you were using the Razor view engine the equivalent view would look like this:

@model Models.PersonDetailsModel
<div>@Model.SomeProperty</div>

Upvotes: 4

Related Questions