How display messagebox 'login failed' in mvc4

Posted by Cpatil1000 under ASP.NET MVC on 4/29/2015 | Points: 10 | Views : 7979 | Status : [Member] | Replies : 1
Hi,
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.

Code:

public ActionResult UserLogin()
{
return View();
}

[HttpPost]
public ActionResult UserLogin(string UserName, string Password)
{

using (ConnectionString userLogic = new ConnectionString())
{
var Result = userLogic.Database.ExecuteSqlCommand(
"SELECT_VISIONMUMBAI_USERNAME_CREDENTIAL @USERNAME, @PASSWORD",
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");
}
else
{
RedirectToAction("UserLogin", "User");
}
}
return View();
}




Responses

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

Up
1
Down
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, http://techfunda.com/Howto/asp-net-mvc/278/insert-record-into-database that shows how to use Model to insert the data.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

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

Login to post response