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.
- public ActionResult SmartRegister(csUser model)
- {
- User us = new User();
- rfSocietyEntities db = new rfSocietyEntities();
- if (ModelState.IsValid)
- {
- int count = db.Users.Where(a => a.Email.Equals(model.Email)).Count();
- if (count == 0)
- {
- us.Admin = model.Admin;
- us.Email = model.Email;
- us.FullName = model.FullName;
- us.Password = model.Password;
- us.PhoneNo = model.PhoneNo;
- db.Users.Add(us);
- db.SaveChanges();
- return RedirectToAction("Dashboard", "Dashboard");
- }
- else
- {
- TempData["msg"] = "<script>alert('Email id already registered.');</script>";
- return View (model);
- }
- }
- else
- {
- TempData["msg"] = "<script>alert('Please Check Data entered or try later.');</script>";
- return View(model);
- }
- }
In your View file, add the below code.
- @Html.Raw(TempData["msg"])
Thats it. Happy Coding. :)