1

I am having a AccountController in which I have an action YNHHConsentForm inside this action I am redirecting to another View using RedirectToAction("Index", "Default"). Now I want to show a message on Index page of Default. I have tried to pass value using ViewBag or ViewData but it remains null and I am unable to use its value on Index.

AccountController

   public ActionResult YNHHConsentForm(YNHHFormVM model)
    {
        if (result == 0 || isSuccess == false)
        {
            model.FormSubmissionMessage = "Something went wrong please try again";
            return View(model);
        }
        else
        {
            SessionItems.IsAuthorizationFormFilled = true;
            //ViewBag.FormSubmissionMessage="Form submitted successfully";
            ViewData["FormSubmissionMessage"] = "Form submitted successfully";
            return RedirectToAction("Index", "Default");
        }
    }

Index(Default)

   @if (ViewData["FormSubmissionMessage"] !=null)
            {
                <div class="alert alert-success">
                    ViewData["FormSubmissionMessage"].ToString()
                </div>
            }

I am using ViewBag and ViewData for first time so not able to figure out where I am doing wrong.

David Mulder
  • 26,123
  • 9
  • 51
  • 114
rupinder18
  • 795
  • 1
  • 18
  • 43

2 Answers2

3

You need to use TempData. In the YNHHConsentForm() method

TempData["FormSubmissionMessage"] = "Form submitted successfully";
return RedirectToAction("Index", "Default");

and access it in the Index() method (and add it to (say) ViewBag so you can then access it in the view.

ViewBag.MyMessage = TempData["FormSubmissionMessage"];

and in the view

<div class="alert alert-success">@ViewBag.MyMessage</div>

For an explanation of the difference between ViewData ViewBag and TempData, refer this answer

Side note: TempData only lasts one redirect so if the user refreshed the browser, the message would not be generated again. You can use .Keep() or .Peek() to solve this if its critical (refer this answer for more detail)

Community
  • 1
  • 1
2

To complement @StephenMueke's answer, I typically create a pair of components for the scenario it seems you're using (displaying system acknowledgements). It consists of:

  1. A static class containing the status/alert types as strings.
  2. A partial view that gets rendered on the layout, so that the message is in a consistent location.

The static class looks something like this:

public class Alert
{
    public static string Error = "alert-error alert-danger";
    public static string Info = "alert-info";
    public static string Warning = "alert-warning";
    public static string Success = "alert-success";

    public static string[] All = new string[]{ Error, Info, Warning, Success };
}

Essentially, these are the Bootstrap alert classes, but they're generic enough to work without Bootstrap. The Alert.Error value contains two "classes" to make it compatible with both version 2 and version 3 of Bootstrap.

The partial view checks TempData for the Alert values, and if found generates a Bootstrap alert:

@foreach (string key in Alert.All)
{
    if (TempData.ContainsKey(key))
    {
        <div class="alert @key">
            <button type="button" class="close" data-dismiss="alert"><i class="fa fa-fw fa-times"></i></button>
            @Html.Raw(TempData[key])
        </div>
    }
}

I use Html.Raw so that I can include markup in the message, if I want to emphasize part of the message:

TempData.Add(Alert.Success, "<b>Thank you!</b> Form submitted successfully!");

would be rendered as:

Thank you! Form submitted successfully!

Tieson T.
  • 20,774
  • 6
  • 77
  • 92