How to create the code behind of Razor page
Asked Answered
L

1

6

When I create a new Razor page in Visual Studio 2017, the separated code behind file did not get created.

This is the screenshot of the problem highlighted.

enter image description here

We can see the Razor page called List.cshtml does not contain the code behind file. Contrast that with the default page about.cshtml, for example.

Lib answered 17/5, 2020 at 14:21 Comment(0)
O
7

1 - Right click on "resturants", add class:

2 - in your case, call it List.cshtml.cs. This will create the code behind. It nows needs wiring up. Open the class that has been created.

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 - In List.cshtml:

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

More information can be found here on modifying the code behind https://learn.microsoft.com/en-gb/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-5.0

Orthodontist answered 15/12, 2020 at 8:48 Comment(2)
Visual Studio 2019 supports grouping of files when your razor page as the name Page1.razor you can add css, cs and js grouped files files, when you call them Page1.razor.css, Page1.razor.ss or Page1.razor.jsPacking
but when you paste a razor and razor.cs into the folder visual studio does not understand that they should be linked?Cirsoid

© 2022 - 2024 — McMap. All rights reserved.