简体   繁体   English

正确设置 razor 页面以访问代码隐藏中的数据

[英]Setup correctly a razor page to have access to data in code-behind

I'm new with Razor Pages and I found out that code can be divided using code-behind file.我是 Razor 页面的新手,我发现可以使用代码隐藏文件来划分代码。 So I created this Index class to initialize my date and print them to the .cshtml file.所以我创建了这个索引 class 来初始化我的日期并将它们打印到.cshtml文件。 But when I try to compile and run it, I get an reror但是当我尝试编译并运行它时,我得到了一个错误

'Object reference not set to an instance of an object.' “对象引用未设置为 object 的实例。”

when I try to use @Model.date .当我尝试使用@Model.date

I understood that the variable are not initialized cause OnGet method is never called.我知道变量没有初始化,因为永远不会调用 OnGet 方法。 But it should be called when the page is requested right?So why variable are never initialize?但它应该在请求页面时调用,对吗?那么为什么变量永远不会初始化? I start the project in localhost with visual studio so i want only to print the data.我在 localhost 中使用 visual studio 启动项目,所以我只想打印数据。 First of all, when I put the code inside the Razor page without using the class Index it worked as I expected.首先,当我将代码放入 Razor 页面而不使用 class Index时,它按我预期的方式工作。 When I tried to move to the next step in the learning path, I am just stuck.当我试图进入学习路径的下一步时,我被卡住了。

@page
@model SportData.Web.Views.Home.Index

<div class="date">
    <h1>Aggiornamento lista in data @Model.date</h1> --> error here
 
</div>
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace SportData.Web.Views.Home
{
    public class Index : PageModel
    {
        List<string> months = new List<string>();
        public DateTime date { get; set; }

        public int num = 4; --> also this give the same error if i try to put in the h1 tag
        public DateTime specificDate { get; set; }
         
        public void OnGet() {
            date = DateTime.Now;
            specificDate = new DateTime(2022, 08, 01);
        }
    }
}

with debug: link to screen of debug result带调试:链接到调试结果屏幕

[SOLUTION]: At the end, the problem was that i create an ASP.NET Web (MVC) project and i tried to implement Razor pattern inside it. [解决方案]:最后,问题是我创建了一个 ASP.NET Web (MVC) 项目,我试图在其中实现 Razor 模式。 So, i understood that is not what i had to do, but simplest way to interact with a Razor Pages in an MVC project is by the Controller and not via Model created as code-beheind.所以,我知道这不是我必须做的,但在 MVC 项目中与 Razor 页面交互的最简单方法是通过 Controller 而不是通过创建为代码隐藏的 Model。

If anyone else want to put other information about MVC and Razor is well accepted.如果其他人想提供有关 MVC 和 Razor 的其他信息,则可以接受。 Thanks.谢谢。

So I created this Index class to initialize my date and print them to the.cshtml file.所以我创建了这个索引 class 来初始化我的日期并将它们打印到 .cshtml 文件。 But when I try to compile and run it, I get an error "'Object reference not set to an instance of an object."但是当我尝试编译并运行它时,出现错误"'Object reference not set to an instance of an object." . . When I tried to move to the next step in the learning path, I am just stuck.当我试图进入学习路径的下一步时,我被卡住了。

Well, you are getting the exception bcause of your incorrect reference on your view @model SportData.Web.Views.Home.Index好吧,由于您对视图@model SportData.Web.Views.Home.Index的引用不正确,您会收到异常

Solution:解决方案:

You should use your reference as @model Index so your view should be as following:您应该使用您的参考作为@model Index ,因此您的视图应如下所示:

@page
@model Index

<div class="date">
    <h1>Aggiornamento lista in data @Model.date</h1>
 
</div>

在此处输入图像描述

You can get more details here 您可以在此处获取更多详细信息

Output: Output:

在此处输入图像描述

Note:笔记:

If you need more implementation sample, you can check our official document here.如果您需要更多实施示例,可以在此处查看我们的官方文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM