在审查了一个旧的Razor应用程序后,我决定更新它,因为无法让部分标签助手工作,我升级到Visual Studio (社区) 2019,因为我能找到的所有信息都从那里指示,并创建了一个新的ASP.NET核心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.Index.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()
{
}
}
}发布于 2020-11-26 04:16:42
PartA.cshtml页面与WebApplication1.Pages.Shared.PartAModel紧密绑定。您需要通过parial标签传递相同的模型
@{var part = new PartAModel;}
<div>
<partial name="PartA" model="@part" /> </div>https://stackoverflow.com/questions/65011726
复制相似问题