ViewDataDictionary 模型类型异常
ViewDataDictionary Model Type Exception
我是 运行 一个有两个主要 table 的应用程序 - TJob 和 TJobHistory。 TJobHistory table 用作对 TJob table 中所做的任何更改的审计 table。我正在尝试更改我的 'details' 视图,以便它显示用户在主页中单击的特定 TJob 条目的 TJobHistory。当我尝试加载任何 TJobs 条目的详细信息页面时,我 运行 出现以下异常:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Planner_App.Models.TJob', but this ViewDataDictionary instance requires a model item of type 'Planner_App.Models.TJobHistory'.
控制器代码
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
//Foreign key pulls//
var TJob = await _context.TJob
.Include(t => t.intCustomer)
.Include(t => t.intDeveloper)
.Include(t => t.intJobStatus)
.Include(t => t.intJobType)
.FirstOrDefaultAsync(m => m.intJobId == id);
if (TJob == null)
{
return NotFound();
}
return View(TJob);
}
查看代码
@model Planner_App.Models.TJobHistory
@{
ViewData["Title"] = "Details";
}
我对 MVC 还是很陌生,所以如果这没有帮助,我深表歉意 - 但我似乎无法弄清楚我在这里做错了什么。如果有人能指出我正确的方向,将不胜感激!
你必须修复你的视图模型
@model Planner_App.Models.TJob
或者如果你真的需要使用 TJobHistory 并且可以将 TJob 转换为 TJobHistory 你可以尝试这样的事情
var model = await _context.TJob
.Include(t => t.intCustomer)
.Include(t => t.intDeveloper)
.Include(t => t.intJobStatus)
.Include(t => t.intJobType)
.Where(m => m.intJobId == id)
Select(i=> new TJobHistory
{
Id=i.Id,
... TJobHistory properties
}).FirstOrDefaultAsync();
if model == null)
{
return NotFound();
}
return View(model);
或者如果 TJobHistory 是 TJob 的副本,也许只要将 _context.TJob 替换为 _context.TJobHistory
就足够了
var tJobHistory = await _context.TJobHistory
....
我是 运行 一个有两个主要 table 的应用程序 - TJob 和 TJobHistory。 TJobHistory table 用作对 TJob table 中所做的任何更改的审计 table。我正在尝试更改我的 'details' 视图,以便它显示用户在主页中单击的特定 TJob 条目的 TJobHistory。当我尝试加载任何 TJobs 条目的详细信息页面时,我 运行 出现以下异常:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'Planner_App.Models.TJob', but this ViewDataDictionary instance requires a model item of type 'Planner_App.Models.TJobHistory'.
控制器代码
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
//Foreign key pulls//
var TJob = await _context.TJob
.Include(t => t.intCustomer)
.Include(t => t.intDeveloper)
.Include(t => t.intJobStatus)
.Include(t => t.intJobType)
.FirstOrDefaultAsync(m => m.intJobId == id);
if (TJob == null)
{
return NotFound();
}
return View(TJob);
}
查看代码
@model Planner_App.Models.TJobHistory
@{
ViewData["Title"] = "Details";
}
我对 MVC 还是很陌生,所以如果这没有帮助,我深表歉意 - 但我似乎无法弄清楚我在这里做错了什么。如果有人能指出我正确的方向,将不胜感激!
你必须修复你的视图模型
@model Planner_App.Models.TJob
或者如果你真的需要使用 TJobHistory 并且可以将 TJob 转换为 TJobHistory 你可以尝试这样的事情
var model = await _context.TJob
.Include(t => t.intCustomer)
.Include(t => t.intDeveloper)
.Include(t => t.intJobStatus)
.Include(t => t.intJobType)
.Where(m => m.intJobId == id)
Select(i=> new TJobHistory
{
Id=i.Id,
... TJobHistory properties
}).FirstOrDefaultAsync();
if model == null)
{
return NotFound();
}
return View(model);
或者如果 TJobHistory 是 TJob 的副本,也许只要将 _context.TJob 替换为 _context.TJobHistory
就足够了var tJobHistory = await _context.TJobHistory
....