Download Logo

ViewBag & friends

Data Passing Between Controller and View Using Form Like POST Method ViewBag.Number and (int)ViewData[“Number”] from controller to View Controller to Controller TempData 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public ActionResult SubmitForm(MyModel model) { if (ModelState.IsValid) { // Process the form data // Store a success message in TempData TempData["Message"] = "Form submitted successfully!"; // Redirect to another action return RedirectToAction("Confirmation"); } return View(model); } public ActionResult Confirmation() { // Retrieve the message from TempData ViewBag.Message = TempData["Message"]; return View(); } 1. Using ViewData ViewData is a dictionary-based way to pass data to the view. It uses the ViewDataDictionary class and is accessed by string keys. ...

August 3, 2024 · 2 min · 374 words · PrashantUnity