How display messagebox 'login failed' in mvc4

Posted by Cpatil1000 under ASP.NET MVC on 4/29/2015 | Points: 10 | Views : 7775 | Status : [Member] | Replies : 1
I have written some coding for login check. But problem is that my message box is not display. if i try below coding then it is getting error.
So when i clik login button then check my condition with if and if condition is failed then display messgebox as like 'Login Failed' else go to next page. I want exactly messagebox not lable type message. as like javascript alert messagebox.


public ActionResult UserLogin()
return View();

public ActionResult UserLogin(string UserName, string Password)

using (ConnectionString userLogic = new ConnectionString())
var Result = userLogic.Database.ExecuteSqlCommand(
new SqlParameter("@USERNAME", UserName),
new SqlParameter("@PASSWORD", Password));

if (Convert.ToInt32(Result) == -1)
ModelState.AddModelError("", "The user login or password provided is incorrect.");
RedirectToAction("UserLogin", "User");
return View();


Posted by: Sheonarayan on: 5/13/2015 [Administrator] HonoraryPlatinum | Points: 25

In your approach, you are returning the error as ModelError that only shows at the place of @Html.ValidationSummary. Also you should not use RedirectToAction method after that, simply return the View() and it should work.

The best way to do this is to create a model with UserName and Password property and use that model in the View so that if any error occurs, you return the model also and the existing username value will be maintained in the web page.

Look at this example, that shows how to use Model to insert the data.


Sheo Narayan

Cpatil1000, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response