Alert Dialog From Controller Without JavaScript In View

In this blog, we will learn how to display an alert dialog from Controller without using JavaScript in the View.

We can show an alert dialog in the browser from Controller without using any JavaScript in the View, which saves our time and makes the popping up of dynamic data way faster.
 
Displaying an Alert Dialog popup can be done from the controller and even from the server-side, but it is very useful when you want to display an alert using much less code.
 
In your Controller, copy the below code just before your return code.
  1. public ActionResult SmartRegister(csUser model)  
  2.  {               
  3.      User us = new User();  
  4.      rfSocietyEntities db = new rfSocietyEntities();  
  5.      if (ModelState.IsValid)  
  6.      {  
  7.          int count = db.Users.Where(a => a.Email.Equals(model.Email)).Count();  
  8.          if (count == 0)  
  9.          {  
  10.              us.Admin = model.Admin;  
  11.              us.Email = model.Email;  
  12.              us.FullName = model.FullName;  
  13.              us.Password = model.Password;  
  14.              us.PhoneNo = model.PhoneNo;  
  15.              db.Users.Add(us);  
  16.              db.SaveChanges();  
  17.              return RedirectToAction("Dashboard""Dashboard");  
  18.          }  
  19.          else  
  20.          {  
  21.              TempData["msg"] = "<script>alert('Email id already registered.');</script>";  
  22.              return View (model);  
  23.          }  
  24.      }  
  25.      else  
  26.      {  
  27.          TempData["msg"] = "<script>alert('Please Check Data entered or try later.');</script>";  
  28.          return View(model);  
  29.      }  
  30. }    
In your View file, add the below code.
  1.   @Html.Raw(TempData["msg"])
Thats it. Happy Coding. :)