【发布时间】:2017-03-23 10:34:36
【问题描述】:
我在控制器中有几个动作方法
这里是第一个,这里我收到 Interview_id 并写在 TempData 中。
[HttpPost]
public ActionResult WelcomeScreen(Interview interview)
{
db.Interview.Add(interview);
db.SaveChanges();
Int32 id = interview.Interview_Id;
TempData["id"] = id;
return RedirectToAction("Index", "Questions");
}
我在TempData["id"] = id;这里设置断点,看到id有值。
这是下一个方法,这里我需要将数据从 TempData 写入表。
[HttpPost]
public ActionResult Index(string question1, string question2, string question3, string question4, string question5, string question6, string question7, string question8, string question9, string question10,Int32 id)
{
QuestionBlock question = new QuestionBlock
{
Question1 = question1,
Question2 = question2,
Question3 = question3,
Question4 = question4,
Question5 = question5,
Question6 = question6,
Question7 = question7,
Question8 = question8,
Question9 = question9,
Question10 = question10,
Interview_Id = (int)TempData["id"],
};
db.QuestionBlocks.Add(question);
db.SaveChanges();
return Json(new { Result = "Success", Message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);
}
但我有错误。
参数字典包含方法“System.Web.Mvc.ActionResult Index”的不可空类型“System.Int32”的参数“id”的空条目
我做错了什么?
更新
获取方法
// GET: Questions
public ActionResult Index()
{
ViewBag.Question1 = new SelectList(db.Questions, "question", "question");
ViewBag.Question2 = new SelectList(db.Questions, "question", "question");
ViewBag.Question3 = new SelectList(db.Questions, "question", "question");
ViewBag.Question4 = new SelectList(db.Questions, "question", "question");
ViewBag.Question5 = new SelectList(db.Questions, "question", "question");
ViewBag.Question6 = new SelectList(db.Questions, "question", "question");
ViewBag.Question7 = new SelectList(db.Questions, "question", "question");
ViewBag.Question8 = new SelectList(db.Questions, "question", "question");
ViewBag.Question9 = new SelectList(db.Questions, "question", "question");
ViewBag.Question10 = new SelectList(db.Questions, "question", "question");
ViewBag.Vacancy = new SelectList(db.Vacancy, "VacancyId", "VacancyName");
return View(db.Questions.ToList());
}
【问题讨论】:
-
您需要显示
IndexGET 方法(POST 方法无关)。既然它需要一个id,那么它应该是return RedirectToAction("Index", "Questions", new { id = interview.Interview_Id });并删除你所有的TempData代码 -
这不是错误消息所指的方法 - 应该是
public ActionResult Index(int id) -
所以它是 Post 方法。我用 Post 方法写表。@StephenMuecke
-
我通过
return RedirectToAction("Index", "Questions", new { id = interview.Interview_Id });解决问题。谢谢@StephenMuecke
标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-4