0

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.

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

That's pretty much what I was thinking. I see what my problem is now, though (sort of). My model is in a different project and it doesn't seem to be visible from the aspx file. I'll have figure out why it's not being seen. Thanks for the clarification on accessing data from the controller in my view. That does help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.