如何将部分 razor 页面加载到 razor 视图中
How to load partial razor page into razor view
我正在尝试用 razor 页面替换我的视图组件,但似乎无法加载部分 razor 页面,因为模型预计会通过,但我的理解是 razor 页面的模型应该在 OnGetAsync 方法中声明。这是我的代码...
剃刀页面
@page "{id:int}"
@model _BackgroundModel
<form method="POST">
<div>Name: <input asp-for="Description" /></div>
<input type="submit" />
</form>
Razor 页面代码隐藏
public class _BackgroundModel : PageModel
{
private readonly IDataClient _dataClient;
public _BackgroundModel(IDataClient dataClient)
{
_dataClient = dataClient;
}
[BindProperty]
public BackgroundDataModel Background { get; set; }
public async Task OnGetAsync(int id)
{
Background = await _dataClient.GetBackground(id);
}
public async Task OnPostAsync()
{
if (ModelState.IsValid)
{
await _dataClient.PostBackground(Background);
}
}
}
剃刀视图
<div class="tab-pane fade" id="client-background-tab">
<div class="row">
<div class="col-sm-12">
@await Html.PartialAsync("/Pages/Client/_Background.cshtml", new { id = 1 })
</div>
</div>
</div>
页面加载错误
InvalidOperationException: The model item passed into the
ViewDataDictionary is of type '<>f__AnonymousType0`1[System.Int32]',
but this ViewDataDictionary instance requires a model item of type
'WebApp.Pages.Client._BackgroundModel'
在此示例中(根据 MS 在其文档中推荐的方法)模型在 OnGetAsync 方法中设置,当请求页面时该方法应为 运行。我也试过 @await Html.RenderPartialAsync("/Pages/Client/_Background.cshtml", new { id = 1 }) 但同样的错误结果。
如何将 razor 页面加载到现有视图中?
Microsoft 确认这无法实现,因此 razor 页面不能用作视图组件的替代品。
查看他们文档的评论...
@RickAndMSFT moderator15 hours ago
@OjM You can redirect to the page, or you can make the core view >code into a partial and call it from both.
Pages are not a replacement for partials or View Components.
我正在尝试用 razor 页面替换我的视图组件,但似乎无法加载部分 razor 页面,因为模型预计会通过,但我的理解是 razor 页面的模型应该在 OnGetAsync 方法中声明。这是我的代码...
剃刀页面
@page "{id:int}"
@model _BackgroundModel
<form method="POST">
<div>Name: <input asp-for="Description" /></div>
<input type="submit" />
</form>
Razor 页面代码隐藏
public class _BackgroundModel : PageModel
{
private readonly IDataClient _dataClient;
public _BackgroundModel(IDataClient dataClient)
{
_dataClient = dataClient;
}
[BindProperty]
public BackgroundDataModel Background { get; set; }
public async Task OnGetAsync(int id)
{
Background = await _dataClient.GetBackground(id);
}
public async Task OnPostAsync()
{
if (ModelState.IsValid)
{
await _dataClient.PostBackground(Background);
}
}
}
剃刀视图
<div class="tab-pane fade" id="client-background-tab">
<div class="row">
<div class="col-sm-12">
@await Html.PartialAsync("/Pages/Client/_Background.cshtml", new { id = 1 })
</div>
</div>
</div>
页面加载错误
InvalidOperationException: The model item passed into the ViewDataDictionary is of type '<>f__AnonymousType0`1[System.Int32]', but this ViewDataDictionary instance requires a model item of type 'WebApp.Pages.Client._BackgroundModel'
在此示例中(根据 MS 在其文档中推荐的方法)模型在 OnGetAsync 方法中设置,当请求页面时该方法应为 运行。我也试过 @await Html.RenderPartialAsync("/Pages/Client/_Background.cshtml", new { id = 1 }) 但同样的错误结果。
如何将 razor 页面加载到现有视图中?
Microsoft 确认这无法实现,因此 razor 页面不能用作视图组件的替代品。
查看他们文档的评论...
@RickAndMSFT moderator15 hours ago @OjM You can redirect to the page, or you can make the core view >code into a partial and call it from both.
Pages are not a replacement for partials or View Components.