如何在Razor页面后面创建代码

时间:2020-05-17 14:21:10

标签: asp.net model-view-controller razor code-behind razor-pages

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

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

enter image description here

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

1 个答案:

答案 0 :(得分:0)

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://docs.microsoft.com/en-gb/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-5.0后面的代码的更多信息