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

前端 未结 4 1418
面向向阳花
面向向阳花 2020-12-05 05:19

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

4条回答
  •  春和景丽
    2020-12-05 06:20

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

    For a view page named 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).

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

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

提交回复
热议问题