如何创建 Razor 页面的后台代码

חוס*_*אמה 5 asp.net model-view-controller code-behind razor razor-pages

当我在Visual Studio 2017中创建新的 Razor 页面时,未创建分离的代码隐藏文件。

这是突出显示的问题的屏幕截图。

在此输入图像描述

我们可以看到调用的 Razor 页面List.cshtml不包含代码隐藏文件。about.cshtml例如,将其与默认页面进行对比。

Ian*_*son 6

1 - 右键单击​​“餐厅”,添加类别:

2 - 在您的情况下,将其命名为 List.cshtml.cs。这将创建后面的代码。现在需要接线。打开已经创建的类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//Add the MVC usings
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

//TODO The namespace will be correct in your code
namespace app_name.Pages.resturants
{
    //public class List
    //TODO correct the model name and inherit PageModel
    public List1Model : PageModel
    {
        public void OnGet()
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

3 - 在 List.cshtml 中:

@page
@model app_name.Pages.resturants.List1Model
@{
}
Run Code Online (Sandbox Code Playgroud)

有关修改https://learn.microsoft.com/en-gb/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-5.0背后的代码的更多信息,请参阅此处