how to add a code-behind page to a view or partial view

24,210

Solution 1

How to add a Code-behind page to a Partial View

Seems this wasn't particularly tricky, and is quite do-able. This answer worked for a Partial ViewUserControl but the same should apply for a Normal MVC ViewPage as well

  1. Add a new Class file with the convention of <view filename & extention>.cs (i.e. view.ascx.cs)

  2. Add using System.Web.Mvc; to the class

  3. Change the class to Inherit from ViewUserControl<>.
    i.e. public class Foo:ViewUserControl

  4. Add the following to the View's header:

    CodeBehind="View.ascx.cs" Inherits="Project.Views.Shared.View"

  5. Copy the files out of the solution and drag back in to re-associate the two together. This may not be necessary in VS 2010+ and MVC 2+.

For this to work with a normal MVC View, you just need to inherit the class from "ViewPage"

Solution 2

I'm not sure why you are creating a code behind file, but if you really really do, then I would consider using the standard webforms approach instead.

I would also look into the basics of MVC to understand why page behinds are not needed.

Another explanation

How to use ASP:Chart without a code-behind (Option B)

Solution 3

To add a codebehind file to your aspx page, while still allowing it to be the target of an MVC view, do the following.

For a view page named Index.aspx...

Replace the following code....

<%@ Page Inherits="System.Web.Mvc.ViewPage" %>

with

<%@ Page CodeFile="Index.aspx.vb" Inherits="Home_Index" %>

Then create a file called Index.aspx.cs (or .vb).

partial class Home_Index : System.Web.Mvc.ViewPage
{...}

or VB

Partial Class Home_Index
    Inherits System.Web.Mvc.ViewPage
    ...
End Class

That's it. The only thing special is using the correct Mvc.ViewPage base class.

Share:
24,210
Andrew Harry
Author by

Andrew Harry

I have been a web developer for the nearly n years. I have a background in graphic design and programming. I program in C# and VB. I can handle most DBA tasks. I prefer ASP.net MVC Personal blog

Updated on July 09, 2022

Comments

  • Andrew Harry
    Andrew Harry almost 2 years

    I notice with the latest version of ASP.NET MVC that a View no longer defaults to having code-behind classes.

    How do I go about adding a code-behind class now to a View or Partial View??

  • Andrew Harry
    Andrew Harry over 15 years
    It's quite simple really. I need to use code behind to populate the Chart series i'm using. It wouldn't let me use a normal MVC approach. This is a REAL world compromise, and it works.
  • Dan Atkinson
    Dan Atkinson over 15 years
    In that case, here is how you can use Chart without a code-behind: code-inside.de/blog-in/2008/11/27/…
  • Andrew Harry
    Andrew Harry over 15 years
    So are you telling me you would prefer to have very messy hard to manage spaghetti code in your html? I think this is very valid use of code behind. It is not controller logic, it isn't presentation code. it is presentation preparation.
  • Dan Atkinson
    Dan Atkinson over 15 years
    You don't have to put all of that code in the view. It's merely put there to show the user that they don't need to use code-behinds to achieve the same thing.
  • Andrew
    Andrew over 14 years
    Thanks for that. I've found a quicker way to associate the two files together is to right-click them, choose Exclude From Project, then with 'Show All Files' selected right-click them again and re-include them.
  • ashes999
    ashes999 over 13 years
    VS2010 associates them as soon as you create the class blah.ascx.cs (in the same directory as the aspx page). Awesome.