简体   繁体   English

我无法在 blazor 服务器应用程序中的 .razor 页面中使用 C# 类

[英]I can not use a C# class in a .razor page, in a blazor server application

I am building a small blazor server application.我正在构建一个小型 blazor 服务器应用程序。

Under Data folder, I have created Author.cs file, which contains a C# class.Data文件夹下,我创建了Author.cs文件,其中包含一个 C# 类。 This is the code of Author.cs file :这是Author.cs文件的代码:

using System;

namespace BlazorApp1.Data
{
    public class Author
    {
        public Author(string id, string fn, string ln,
            string ph, string city)
        {
            this.id = id;
            this.firstName = fn;
            this.lastName = ln;
            this.phoneNumber = ph;
            this.city = city;
        }

        public string id { get; set; }

        public DateTime Date { get; set; }

        public string phoneNumber { get; set; }

        public string firstName { get; set; }

        public string lastName { get; set; }

        public string city { get; set; }
    }
}

Also, under Pages folder, I have created Authors.razor file.另外,在Pages文件夹下,我创建了Authors.razor文件。 This is the code of Authors.razor file :这是Authors.razor文件的代码:

@page "/authors"
@inject NavigationManager NM

<h3>Authors</h3>
<hr />

<div class="col-12">
    @foreach (Author author in AuthorList)
    {
        <div class="col-12 row">
            <div class="col-2">
                <NavLink href=@string.Format("authors/authordetails/{0}", @author.id)>
                    @author.id
                </NavLink>
            </div>
            <div class="col-2">@author.firstName @author.lastName </div>
        </div>
    }
</div>

@code {

    public List<Author> AuthorList { get; set; }
    public AuthorService authorService { get; set; }

    protected override void OnInitialized()
    {
        authorService = new AuthorService();
        AuthorList = authorService.GetAuthors();
    }
}

The problem is, in foreach loop, Author is underlined in red, and Visual Studio 2019 says:问题是,在foreach循环中, Author用红色下划线标出,Visual Studio 2019 说:

CS0246: The type or namespace name 'Author' could not be found (are you missing a using directive or an assembly reference?) CS0246:找不到类型或命名空间名称“作者”(您是否缺少 using 指令或程序集引用?)

What should I do so I can use Author class in this .razor page?我应该怎么做才能在这个 .razor 页面中使用Author类?

只需在组件中添加命名空间 BlazorApp1.Data。

暂无
暂无

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

相关问题 Blazor 应用程序将变量从 razor 页面传递到 c# - Blazor application passing variables from razor page to c# 如何将数据从 index.razor 文件传输到 C# blazor 中的另一个 razor 文件 - How can I transfer data from index.razor file to another razor file in C# blazor 如何像在 Blazor @inject ClassName classObject - How can I use/inject a service in a “normal” c# class like in Blazor @inject ClassName classObject 如何使用 blazor 服务器将 UI 代码从 razor 页面拆分到 class - How split UI code from razor page to class with blazor server 如何在 ASP.NET Blazor 中定义全局 C# 代码类,哪些函数可以从相关剃须刀页面的每个部分类中调用? - How can i define gloabal C# code classes in ASP.NET Blazor which functions can be called from each partial class of the related razor pages? 如何在 Blazor 中创建 Razor 页面列表 - How can I create a List of Razor Page in Blazor Blazor 服务器端,外部注册按钮在.razor页面 - Blazor Server side, ExternalRegister buttons at .razor page 如何设计一个包装器 class 来使用 C++ 应用程序以使用 C# Z5884E40D796370BE39AZ109ADF? - How can I design a wrapper class for consuming a C++ application to use a C# DLL? 将 C# Blazor 应用程序部署到 AzureWebSites,但索引是常规 HTML 页面 - Deploying a C# Blazor application to AzureWebSites but the index is a regular HTML page 在普通 c# 代码中访问 blazor (wasm) 注入对象,而不是 razor 页面 - Accessing blazor (wasm) injected objects in plain c# code as opposed to razor page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM