Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

asp.net mvc - how to add a code-behind page to a view or partial view

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??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...