我的部分页面无法从PageModel .NET 2.2获取正确的模型

时间:2018-12-06 20:22:56

标签: c# .net html5 razor-pages

我一直在尝试在导航药丸印刷机上渲染局部图像,只要使用pagemodel,我就能看到局部图像和字段。我目前正在使用Razor Pages .NET2.2。我的问题是如何获取部分页面以将PageModel与部分页面一起使用。我在“汽车模型”中还有更多字段,希望在部分页面中使用它们。似乎想要ViewDataDictionary中的PageModel。那么如何将PageModel传递给它,以便可以在部分页面中使用PageModel?

我的页面模型(CarEditModel)方法和模型

   [BindProperty]
   public Car Data { get; set; }

   public PartialViewResult OnGetCarPartial(int Id)
   {
       Data = Repo.GetById(Id);
       return Partial("_EditCarPartial",Data);
    }

我的部分页面

@model CARORDR.Areas.Auto.Pages.Edit.CarEditModel
@{
  Layout = null;
 }
<div class="panel panel-default">
<div class=" panel-heading">
 <div class="form-group">
 <label asp-for="Data.CarId" class="control-label">Car ID</label>
 <input asp-for="Data.CarId" type="text" class="form-control" value="" />
 <span asp-validation-for="Data.CarId" class="text-danger"></span>
 <label asp-for="Data.CarName" class="control-label">Car Name</label>
 <input asp-for="Data.CarName" type="text" class="form-control" >
 <span asp-validation-for="Data.CarName" class="text-danger"></span>
 </div>
 </div>

我的页面

@Page
@model CARORDR.Areas.Auto.Pages.Edit.CarEditModel
@{
  Layout = Layout;
 }
<div id="partialPagegoesHere"></div>

我得到的例外情况

System.InvalidOperationException Message =传递给ViewDataDictionary的模型项的类型为'CARORDR.Models.Car',但是此ViewDataDictionary实例需要类型为'CARORDR.Areas.Auto.Pages.Edit.CarEditModel'的模型项。

更新:这是我找到的解决此问题的方法

public PartialViewResult OnGetCarPartial(int Id)
   {
       Data = Repo.GetById(Id);
       return Partial("_EditCarPartial",this);
    }

0 个答案:

没有答案