asp.net-mvc 如何将代码隐藏页面添加到视图或部分视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/680448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:26:54  来源:igfitidea点击:

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

asp.net-mvcviewscode-behind

提问by Andrew Harry

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

我注意到最新版本的 ASP.NET MVC 视图不再默认具有代码隐藏类。

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

我现在如何将代码隐藏类添加到视图或部分视图?

回答by Andrew Harry

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 ViewUserControlbut the same should apply for a Normal MVC ViewPageas well

似乎这不是特别棘手,并且是完全可行的。这个答案的部分工作ViewUserControl,但同样应该适用于一个普通MVCViewPage以及

  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+.

  1. 使用<view filename & extention>.cs(ie view.ascx.cs)的约定添加一个新的类文件

  2. 添加using System.Web.Mvc;到班级

  3. 将类更改为从 继承ViewUserControl<>
    IEpublic class Foo:ViewUserControl

  4. 将以下内容添加到视图的标题:

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

  5. 将文件从解决方案中复制出来并拖回以将两者重新关联在一起。这在 VS 2010+ 和 MVC 2+ 中可能不是必需的。

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

要使其与普通 MVC 视图一起使用,您只需要从“ViewPage”继承该类

回答by Dan Atkinson

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.

我不确定你为什么要创建一个代码隐藏文件,但如果你真的这样做了,那么我会考虑使用标准的 webforms 方法。

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

我还将研究 MVC 的基础知识,以了解为什么不需要后台页面。

Another explanation

另一种解释

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

如何在没有代码隐藏的情况下使用 ASP:Chart(选项 B)

回答by Dan Atkinson

Ok, I have verified the solution, here is something that you need to note:

好的,我已经验证了解决方案,这里有一些你需要注意的地方:

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

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

In your case, you need to change "Project.Views.Shared.View" based on your namespace and classname, and in order to access the control in the code-behind, you have to manually add declaration in code-behind. In my case, I need to initialize the gigaSoft proEssential control:

在您的情况下,您需要根据您的命名空间和类名更改“Project.Views.Shared.View”,并且为了访问代码隐藏中的控件,您必须在代码隐藏中手动添加声明。就我而言,我需要初始化 gigaSoft proEssential 控件:

public class gigaTest2 : ViewUserControl
{
    protected global::Gigasoft.ProEssentials.PegoWeb PegoWeb1;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Set Titles 
        PegoWeb1.PeString.MainTitle = "Hello ASP.NET";
        PegoWeb1.PeString.SubTitle = "";

        // One simple way of passing data, data binding also possible. //' 
        PegoWeb1.PeData.Subsets = 1;
        PegoWeb1.PeData.Points = 6;
        PegoWeb1.PeData.Y[0, 0] = 10;
        PegoWeb1.PeData.Y[0, 1] = 30;
        PegoWeb1.PeData.Y[0, 2] = 20;
        PegoWeb1.PeData.Y[0, 3] = 40;
        PegoWeb1.PeData.Y[0, 4] = 30;
        PegoWeb1.PeData.Y[0, 5] = 50;

        // Set style of chart and a few other properties //' 
        PegoWeb1.PePlot.Method = Gigasoft.ProEssentials.Enums.GraphPlottingMethod.Bar;
        PegoWeb1.PePlot.Option.GradientBars = 8;
        PegoWeb1.PeFont.FontSize = Gigasoft.ProEssentials.Enums.FontSize.Large;
    }

回答by Carter Medlin

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

要将代码隐藏文件添加到您的 aspx 页面,同时仍允许它成为 MVC 视图的目标,请执行以下操作。

For a view page named Index.aspx...

对于名为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).

然后创建一个名为Index.aspx.cs(或.vb)的文件。

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

or VB

或 VB

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

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

就是这样。唯一特别的是使用正确的Mvc.ViewPage基类。