1

i am new to MVC, i want to create a simple Login Control. I have write the below code in controller:

public ActionResult LoginControl(String UserName, String Password)
    {
        string result = "";
        if (UserName == "ram" && Password == "ram123")
        {
            result = "1";
        }
        if (result == "1")
        {
            return View("LoginControl");
        }
        else
        {
            return View("Index", "Login");
        }
    }

Now what i want to do is: if the UserName and password does not match, it will show me error that "UserName or password does not matched or user does not exists.", please help me how can i do it.

tereško
  • 58,060
  • 25
  • 98
  • 150
Ram
  • 1,131
  • 10
  • 28
  • 52

2 Answers2

3

You could add an error to the ModelState:

else
{
    ModelState.AddModelError("", "Invalid username or password");
    return View("Index", "Login");
}

and then inside the corresponding view use the ValidationSummary helper to display the error:

@Html.ValidationSummary()
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Can you suggest me one thing more: I am passing the static values from button's click as : i want to take it from the text boxes how can i do it – Ram Aug 07 '13 at 07:35
  • Don't use a button click. Use an Html.BeginForm with a submit button. This way the values will automatically be sent to the controller action when the form is submitted. – Darin Dimitrov Aug 07 '13 at 07:37
1

You could pass your result to the view and than display there. To do this create a TempData key and pass your result there and in the view you can get the value of the tempdata key and than show there.

Here is an example on how to use it: ViewBag, ViewData and TempData

Community
  • 1
  • 1