Eugene Sukhomlin Eugene Sukhomlin -4 years ago 161
ASP.NET (C#) Question

Pass data to other ActionMethod in controller

I have several action methods in controller

Here is first , here I receive Interview_id and write it in TempData.

public ActionResult WelcomeScreen(Interview interview)
Int32 id = interview.Interview_Id;
TempData["id"] = id;
return RedirectToAction("Index", "Questions");

I set breakpoints here
TempData["id"] = id;
and see that
has value.

Here is next method, here I need to write data from TempData to table.

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"],
return Json(new { Result = "Success", Message = "Saved Successfully" }, JsonRequestBehavior.AllowGet);

But I have error.

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index

What I doing wrong?


Get method

// 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());

Answer Source

Just Pass the id to index and you good to go

 return RedirectToAction("Index", "Questions", int id here);`
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download