如何将代码隐藏页面添加到视图或部分视图 [英] how to add a code-behind page to a view or partial view
问题描述
我注意到最新版本的 ASP.NET MVC 视图不再默认具有代码隐藏类.
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??
推荐答案
如何将代码隐藏页面添加到局部视图
看起来这并不是特别棘手,而且相当可行.此答案适用于 Partial ViewUserControl
但同样适用于普通 MVC ViewPage
以及
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
添加一个新的类文件,约定为
(即.cs view.ascx.cs
)
在类中添加using System.Web.Mvc;
将类更改为从 ViewUserControl<>
继承.
即 public class Foo:ViewUserControl
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"
将文件从解决方案中复制出来,然后再拖回来以将两者重新关联在一起.这在 VS 2010+ 和 MVC 2+ 中可能不是必需的.
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+.
为了使用普通的 MVC 视图,您只需要从ViewPage"继承类
For this to work with a normal MVC View, you just need to inherit the class from "ViewPage"
这篇关于如何将代码隐藏页面添加到视图或部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!