【发布时间】:2021-03-08 17:23:30
【问题描述】:
在查看了旧的 Razor 应用程序后,我决定对其进行更新,但无法使部分标签助手工作,我升级到 Visual Studio (Community) 2019,因为我可以从那里找到所有信息,并制作了一个新的实际包含 Pages 文件夹的 ASP.NET Core 5.0 Razor 项目。
现在尝试使用使用部分的布局后,我收到以下错误:
InvalidOperationException:传递到 ViewDataDictionary 的模型项属于“WebApplication1.Pages.IndexModel”类型,但此 ViewDataDictionary 实例需要一个“WebApplication1.Pages.Shared.PartAModel”类型的模型项。
请告诉我我错过了什么简单的事情使这项工作奏效。
结果应该只有 3-5 个文件,所以我将在这里举例说明:
Index.cshtml:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<p>
This is the parent content.
</p>
</div>
_Layout.cshtml:
<!DOCTYPE html>
<html lang="en">
<body>
<div>
@RenderBody()
</div>
<div>
<partial name="PartA" />
</div>
</body>
</html>
PartA.cshtml:
@page
@model WebApplication1.Pages.Shared.PartAModel
@{
}
This is where the child content goes.
索引.cshtml.cs:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
}
}
PartA.cshtml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApplication1.Pages.Shared
{
public class PartAModel : PageModel
{
public void OnGet()
{
}
}
}
【问题讨论】:
标签: visual-studio-2019 razor-pages asp.net-core-5.0