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

5
当我在Visual Studio 2017中创建一个新的Razor页面时,分离的代码后置文件没有被创建。
这是突出显示此问题的屏幕截图。
我们可以看到名为List.cshtml的Razor页面没有包含代码后置文件。与默认页面about.cshtml相比较。
1个回答

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()
        {
        }
    }
}

3 - 在 List.cshtml 中:

@page
@model app_name.Pages.resturants.List1Model
@{
}

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


Visual Studio 2019支持文件分组,当您的Razor页面命名为Page1.razor时,您可以添加CSS、CS和JS分组文件,当您将它们命名为Page1.razor.cssPage1.razor.ssPage1.razor.js时。 - KargWare
但是,当您将Razor和razor.cs粘贴到文件夹中时,Visual Studio并不理解它们应该链接在一起? - Paul McCarthy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接