【发布时间】:2020-11-08 20:42:18
【问题描述】:
我的警报课程是
namespace RazorComponents.Models
{
public class Alert
{
public string Title { get; set; }
public string Text { get; set; }
public bool Dismissible { get; set; }
}
}
我的 AlertComponent 是
namespace RazorComponents.Test
{
public partial class AlertComponent : ComponentBase
{
public Alert Alert { get; set; }
}
}
以下观点
@using Models
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>@Alert.Title</strong> You should check in on some of those fields below.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
返回编译时错误
C# An object reference is required for the non-static field, method, or property@Alert.Title
这很正常,但我如何将 Alert 类作为模型传递给视图?
至少在 MVC 中我们可以选择@model Alert 并通过async Task<IViewComponentResult> InvokeAsync 传递模型
【问题讨论】: