3

I want to show a successfully submit message on the Index page when user successfully submit on the Register page.

My Register action includes:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveRegister(AirlineWebApplication.Models.User User, HttpPostedFileBase file)
{
    db.Users.Add(User);
    db.SaveChanges();
    return RedirectToAction("Index");
}

My Register view as follows includes.

<section class="registersection">
  <div class="container-fluid">
    <div class="row">
      <div class="signupForm">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
          @using (Html.BeginForm("SaveRegister", "Users", FormMethod.Post)) {
          @Html.ValidationSummary(true)
          @Html.AntiForgeryToken()
          <div class="editor-field">
            <label>Name
                   @Html.EditorFor(model => model.user_name)
            </label>
            @Html.ValidationMessageFor(model => model.user_name)
          </div>
          <div class="editor-field">
            <label>Password
                   @Html.EditorFor(model => model.password)
            </label>
            @Html.ValidationMessageFor(model => model.password)
          </div>
          <p>
            <input type="submit" value="Register" />
          </p>
          }
        </div>
      </div>
    </div>
  </div>
</section>

How can I display the successful message "You have registered successfully" on Index page when I press the Register button?

5
  • 1
    This is a question too broad, please clarify what exactly you're trying to achieve and what's not working. By the way, you've got an unmatched </label> in line 42/ Apr 1, 2018 at 18:03
  • you want to display as popup message ? or just to display message on the index page it self? Apr 1, 2018 at 18:17
  • I want a pop up message. Apr 1, 2018 at 18:28
  • do you have Bootstrap?
    – derloopkat
    Apr 1, 2018 at 18:35
  • yes I have bootstrap. Apr 1, 2018 at 18:49

2 Answers 2

5

There are many ways to do that. For example in your controller use TempData to know where you are coming from:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveRegister(AirlineWebApplication.Models.User User,
    HttpPostedFileBase file)
{

    db.Users.Add(User);
    db.SaveChanges();
    TempData["Referrer"] = "SaveRegister";
    return RedirectToAction("Index");
}

In your Index view:

@if((string)TempData["Referrer"] == "SaveRegister")
{
    <div class="alert alert-success">
      <strong>You have registered successfully</strong>
    </div>
}

Unlike ViewBag, TempData would persist to the next request so that it's suitable for redirecting to a different page and retrieving the value from there.

2

When you create a new project in Visual Studio, you can see examples of how to display confirmation messages.

You can create a new view, say RegisterConfirmation and then in your controller, change the last line from:

return RedirectToAction("Index");

to:

return View("RegisterConfirmation");

EDIT Displaying the message on the main Index page is not recommended. If you want to do so, you need to pass the message to the Index page:

string message = "You have registered successfully";
return RedirectToAction("Index", "Home", new { m = message });

Then in the controller of the index page, you need to grab the message from the query string and add it to the model or the ViewBag:

public ActionResult Index(string m) {
    ViewBag.Message = m;
}

And finally on your index view, you can display the message in a div wherever you want on the page:

<div>@ViewBag.Message</div>

Alternatively, you can make the div a popup. This is pretty simple with CSS.

If you don't want to use the query string (which is probably better), you can use TempData instead:

TempData["Message"] = "You have registered successfully";
return RedirectToAction("Index", "Home");

And in the Index view:

<div>@TempData["Message"]</div>
4
  • But I want to display the message on the main "Index" page. Apr 1, 2018 at 18:08
  • You need to mention that clearly in your question. I will update my answer. Apr 1, 2018 at 18:09
  • why do you say that the index page is a poor choice for the success message? I'm curious because I was just planning to do that, but I don't want to make bad decisions! Mar 21 at 15:20
  • 1
    @KirstieBallance It all depends on your design. In general, you want to display a message to the user in a place relevant to where the action had happened. If the registration is a popup on the index page and cannot be accessed from any other page, then a popup message on the index page is good. However, in most cases, the registration is a full page (not popup) and/or can be accessed from several pages (e.g. the login page), so redirecting the user to the index page can lead to poor user experience. Some websites redirect you to the login page upon registration, but still after the message. Mar 22 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.