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
Add a new Class file with the convention of <view filename & extention>.cs
(i.e. view.ascx.cs
)
Add using System.Web.Mvc;
to the class
Change the class to Inherit from ViewUserControl<>
.
i.e. public class Foo:ViewUserControl
Add the following to the View's header:
CodeBehind="View.ascx.cs" Inherits="Project.Views.Shared.View"
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"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…