markoos
markoos

Reputation: 125

How to detect what is the model of the view and where its getting populated?

I'm learning a C# ASP.NET MVC application and I'm having a hard time walking backwards from the view to the code that executes a stored procedure that populates the model class. This is complicated (for me) by the fact that it uses Castle for IoC, so I can't just search for new [Classname].

Any help is appreciated.

I'll post a more in-depth example soon, but for now I'll simply explain it this way:

One has a View which refers to a @model. That model is a class that was populated via a Controller by calling a stored procedure (or executing inline SQL) and passed to the View.

In simplest terms, my question is:

Upvotes: 0

Views: 1466

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125257

How do I figure out which class @model refers to?

The same way that you can go to the definition of a code element in .cs files, you can do that in .cshtml files as well. Click on the name of the type in front of @model and press F12 or Right click → Go To Definition to see the definition of the type or choose Go To Implementation to see the implementation if available.

If you don't have @model the view class is deriving from WebViewPage<dynamic> the model for the view is dynamic. If you have @model Something the view class is deriving from WebViewPage<Something> and the model for the view is Something.

Assuming you have a SampleModel class in SampleProject.Models namespace, the following ways are some ways that you can declare @model for the Index.cshtml view which is located in Home folder, under Views folder:

Without any @model

<div> Hello! </div>

FullName in View

@model SampleProject.Models.SampleModel
<div> Hello! </div>

Using and Name in View:

@using SampleProject.Models
@model SampleModel
<div> Hello! </div>

Namespace in web.config and Name in View

Having a namespace entry inweb.config in root of the project, or in Views folder or in your view folder (in the example, Home folder):

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      ...
      <add namespace="SampleProject.Models" />
      ...
    </namespaces>
  </pages>
</system.web.webPages.razor>

And the name in view file:

@model SampleModel
<div> Hello! </div>

How do I find the place where that class got populated with data before it got passed to the View?

In short, at design time, the view doesn't know where its data come from, while action knows which view is going to return. At run-time, view knows which action has returned it.

Design time

In MVC, the View doesn't belong to a controller and any action of a controller can return the view simply. So View at design time doesn't have a parent controller/action, while the action knows which view they are going to return.

However in simple cases, which you have a 1-1 relation between views and actions, a view in the following path Views\Home\Index usually is returned by Index action of HomeController class.

In simple cases, when you are coding in view, if you Right Click and choose Go To Controller, it goes to the controller class based on the folder name of the view. When you are coding in controller and Right Click in body of name of an Action and choose Go To View, it goes to the view having the same name as your action in a folder with the same name as your controller (without controller suffix) under views folder.

Run-time

At run-time, each view knows what controller/action has returned it:

@ViewContext.RouteData.Values["controller"]
@ViewContext.RouteData.Values["action"]
@ViewContext.Controller

Upvotes: 2

Related Questions