[英]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.