如何在视图或部分视图中添加代码后页?

时间:2023-01-19 16:34:58

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

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

4 个解决方案

#1


23  

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

这似乎不是特别棘手,而且相当可行。这个答案适用于部分ViewUserControl,但同样适用于普通的MVC视图页面

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

    添加一个具有 约定的新类文件。cs(即view.ascx.cs)

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

    使用System.Web.Mvc添加;到类

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

    将类更改为从ViewUserControl<>继承。即公共类Foo:ViewUserControl

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

    在视图的页眉中添加以下内容:

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

    后台代码= " View.ascx。cs 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+.

    将文件从解决方案中复制出来,并将其拖回,以便将两者重新关联起来。这在VS 2010+和MVC 2+中可能没有必要。

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

要使用普通的MVC视图,只需从“ViewPage”继承类

#2


4  

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:没有代码隐藏的图表(选项B)

#3


2  

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

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

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

后台代码= " View.ascx。cs 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 . view . shared”。根据名称空间和类名查看“,为了访问代码隐藏中的控件,您必须手动添加代码隐藏中的声明。在我的情况下,我需要初始化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;
    }

#4


1  

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.ViewPage base class.

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

#1


23  

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

这似乎不是特别棘手,而且相当可行。这个答案适用于部分ViewUserControl,但同样适用于普通的MVC视图页面

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

    添加一个具有 约定的新类文件。cs(即view.ascx.cs)

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

    使用System.Web.Mvc添加;到类

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

    将类更改为从ViewUserControl<>继承。即公共类Foo:ViewUserControl

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

    在视图的页眉中添加以下内容:

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

    后台代码= " View.ascx。cs 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+.

    将文件从解决方案中复制出来,并将其拖回,以便将两者重新关联起来。这在VS 2010+和MVC 2+中可能没有必要。

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

要使用普通的MVC视图,只需从“ViewPage”继承类

#2


4  

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:没有代码隐藏的图表(选项B)

#3


2  

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

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

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

后台代码= " View.ascx。cs 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 . view . shared”。根据名称空间和类名查看“,为了访问代码隐藏中的控件,您必须手动添加代码隐藏中的声明。在我的情况下,我需要初始化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;
    }

#4


1  

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.ViewPage base class.

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