问题描述
The model item passed into the dictionary is of type '<>f__AnonymousType3`1[system.int64]',but this dictionary requires a model item of type 'MVCProject.Models.AddEmployee'.
场景:
视图将使用num(如果已传递)加载。我有另一个方法HttpPost,它在返回View时给出错误。请参见下面的代码。
public ActionResult AddEmployee(int? num)
{
AddEmployee e = new AddEmployee();
//Business logic
return View(e);
}
[HttpPost]
public ActionResult AddEmployee(AddEmployee model)
{
//Add Employee logic
return View("AddEmployee",new { num = model.EmpNum });
}
解决方法
您正在返回视图,但实际上并未重定向。强烈建议使用Google搜索,并遵循POST Redirect GET模式。例如:
[HttpPost]
public ActionResult AddEmployee(AddEmployee model)
{
if (ModelState.IsValid)
{
//Add Employee logic
var newEmployeeNumber = 1223; // Gotten from your add employee logic
return RedirectToAction("AddEmployee",new { num = newEmployeeNumber });
}
return View(model);
}