I’m experiencing some craziness that I just can’t figure out.
I’ve created the following class:
public abstract class AbstractView<T> : ViewPage<T> where T : class
which gives me some useful helpers and I’m using it in my views like this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="Project.Web.Mvc.AbstractView<Project.Domain.Entities.Example>" %>
This all works fine. Now I’ve got to make a view which needs lots of complex rendering code, so I want to give my view a codebehind, so I can put all this presentational logic in it.
However, when I do:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="Project.Web.Views.Examples.View" CodeBehind="~/Views/Examples/View.aspx.cs" %> public class View : Project.Web.Mvc.AbstractView<Project.Domain.Entities.Example>
Model is always null when I debug the view, which subsequently gives me:
"The view 'View' or its master could not be found. The following locations were searched..."
What’s going on?
P.S. don’t tell me codebehinds are evil. I’m writing presentation specific logic, and lots of it. It’s not going inline in the aspx.
You could try using a code-alongside. By that I mean a class deriving from
AbstractView<T>that is only used by this one view. It’s virtually the same concept as a code-behind. Then just point at that instead of usingAbstractView<T>.Also, your Model won’t be hooked up until OnLoad (could be earlier, I’ve never done it myself) so that could be your problem if you’re trying to use it in the constructor.
Otherwise you might want to look at using a ViewModel so that instead of you passing what I assume are raw domain objects, you do the custom formatting in the ViewModel and pass that to
AbstractView<T>.