Display success message to user

Question

I am using ASP.net core razor engine. I am making a registration and I want to display to the user a success message when the registration had been successful.
Here is my code

my .cs file

namespace login.Controllers
{
    public class HomeController : Controller
    {

        private readonly UserFactory userFactory;

        public HomeController(UserFactory user) {
            userFactory = user;
        }

        // GET: /Home/
        [HttpGet]
        [Route("")]
        public IActionResult Index()
        {
            ViewData["message"] = false;
            return View();
        }

        [HttpPost]
        [Route("")]
        public IActionResult Register(Home model)
        {
            if(!ModelState.IsValid)
            {
                return View("Index", model);
            }
            PasswordHasher<Home> Hasher = new PasswordHasher<Home>();
            model.Password = Hasher.HashPassword(model, model.Password);
            userFactory.Add(model);
            ViewData["message"] = true;
            return RedirectToAction("Index");
        }
    }
} 

my html file

@model login.Models.Home

@using(Html.BeginForm("Register","Home"))
{
    <h1>Please Register</h1>
    <p>
       <label>Your First Name</label>
        @Html.TextBoxFor(s=>s.FirstName)
        @Html.ValidationMessageFor(s => s.FirstName)
    </p>
    <p>
        <label>Your Last Name</label>
        @Html.TextBoxFor(s=>s.LastName)
        @Html.ValidationMessageFor(s => s.LastName)
    </p>
     <p>
        <label>Your Email</label>
        @Html.TextBoxFor(s=>s.Email)
        @Html.ValidationMessageFor(s => s.Email)
    </p>
    <p>
        <label>Your Password</label>
        @Html.TextBoxFor(s=>s.Password)
        @Html.ValidationMessageFor(s => s.Password)
    </p>
    <p>
        <label>Confrim Password</label>
        @Html.TextBoxFor(s=>s.PasswordConfirmation)
        @Html.ValidationMessageFor(s => s.PasswordConfirmation)
    </p>
    <input type="submit" name="submit" value="Register!"/>
}
 @if(ViewData["message"] == "true")
      {
          <p>Success</p>
      }

I also changed to TempData["message"] == "false" so see if I would get any message in my html. But the message does not renders in my html


Show source
| html   | c#   | asp.net-mvc   | message   2017-01-02 19:01 3 Answers

Answers to Display success message to user ( 3 )

  1. 2017-01-02 19:01

    In the controller, you're setting ViewData, then checking TempData in the view. Update this to match and your code should work.

  2. 2017-01-02 19:01

    You have two problems with your code.

    First is that in the controllers you use ViewData and in the View you use TempData - they are different containers. Use one of them consistently.

    You need to also be aware that ViewData only lives during the request so when you do the redirect it will go away - it looks like the more suitable would be TempData here.

    Second is that you always set the ViewData["message"] to false in your Index action and after you register you do the redirect to Index so even if you would check the same container in your view it would yield false and the message would not show. You need to set the value to false only after you render it in your view.

  3. 2017-01-02 20:01

    you can utility this code:

    code cs.

    namespace login.Controllers
    {
    public class HomeController : Controller
    {
    
        private readonly UserFactory userFactory;
    
        public HomeController(UserFactory user) {
            userFactory = user;
        }
    
        // GET: /Home/
        [HttpGet]
        [Route("")]
        public IActionResult Index()
        {
    
            return View();
        }
    
        [HttpPost]
        [Route("")]
        public IActionResult Register(Home model)
        {
            if(!ModelState.IsValid)
            {
                return View("Index", model);
            }
            PasswordHasher<Home> Hasher = new PasswordHasher<Home>();
            model.Password = Hasher.HashPassword(model, model.Password);
            userFactory.Add(model);
            ViewBag.message = "Success";
            return View();
           }
        }
    } 
    

    code razor

    @model login.Models.Home
    
    @using(Html.BeginForm("Register","Home"))
    {
     <h1>Please Register</h1>
     <p>
       <label>Your First Name</label>
        @Html.TextBoxFor(s=>s.FirstName)
        @Html.ValidationMessageFor(s => s.FirstName)
     </p>
    <p>
        <label>Your Last Name</label>
        @Html.TextBoxFor(s=>s.LastName)
        @Html.ValidationMessageFor(s => s.LastName)
    </p>
     <p>
        <label>Your Email</label>
        @Html.TextBoxFor(s=>s.Email)
        @Html.ValidationMessageFor(s => s.Email)
    </p>
    <p>
        <label>Your Password</label>
        @Html.TextBoxFor(s=>s.Password)
        @Html.ValidationMessageFor(s => s.Password)
    </p>
    <p>
        <label>Confrim Password</label>
        @Html.TextBoxFor(s=>s.PasswordConfirmation)
        @Html.ValidationMessageFor(s => s.PasswordConfirmation)
    </p>
    <input type="submit" name="submit" value="Register!"/>
    }
    @if(!string.IsNullOrEmpty(ViewBag.message))
      {
          <p>@ViewBag.message</p>
      }
    

Leave a reply to - Display success message to user

◀ Go back