c# - asp.net MVC HTTPPost 返回视图
问题描述
在 HTTPPost 之后重定向到查看后出现以下错误。
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,它在返回视图上给出错误。请看下面的代码。
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 });
}
解决方案
您正在返回视图而不是实际重定向。强烈推荐谷歌搜索并遵循 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);
}
推荐阅读
- batch-file - 在线/启动时检查 vpn 连接的批处理文件
- r - 从部分信息产生一个完整的邻接矩阵
- netlogo - 使用 netlogo 获取全局变量的颜色
- ruby - 我可以创建一个函数来将整个字母表推送到数组中吗?
- python - 计算文本文件中的每个单词并输出成本
- angular - 在 Angular7 中使用 laravel 5.7 API 的问题
- r - 运行 R Shiny 应用程序时如何在数据表函数中编辑列名?
- ssl - Google Domains 的 Heroku 裸域问题
- gradle - Groovy v2.5.4 的 INDY 版本在哪里?
- web3js - Web3JS - 前端或后端