I'm trying to pass a success message from my Controller action to the View. However, any solutions i try or come across googleing doesn't seem to work. After an hour trying to figure out what I'm doing wrong I'll ask here.
In the exampel i use ViewBag but I've tried with TempDate["MyMessage"] = "Some message"; still same.. Value is always null in View...
Controller
public ActionResult EditSupplier(Supplier supplier)
{
try
{
if (ModelState.IsValid)
{
this._service.Update(supplier);
ViewBag.UserMessage = "Supplier updated successfully";
}
}
catch (Exception ex)
{
ModelState.AddModelError(String.Empty, ex.Message);
TempData["UserMessage"] = "Error, supplier couldn't be updated";
return View("Error");
}
return RedirectToAction("Supplier", new { id = supplier.SupplierId });
}
View
@if (ViewBag.UserMessage != null)
{
<p>@ViewBag.UserMessage.ToString()</p>
}