0

I tried looking on other examples of people like this one: example. But It's still not working.

My class looks like this:

    [Required]
    public string UserName { get; set; }
    [Required]
    public string Password { get; set; }

Controller:

    public ActionResult Login(string UserName, string password)
    {
        return View();
    }

My View is based on the class.. but still it lets me press the submit button even though nothing is typed in.

Help?

Community
  • 1
  • 1
oneman
  • 41
  • 1
  • 6
  • 1
    Have you got client side validation turned on? If not, it will go back to the server to validate and your ModelState will fail validation as a result – LDJ Mar 02 '17 at 14:08
  • 1
    As far as I see you pass attributes UserName and password directly to your Login action. Try passing the Model that contains the required fields instead. – Lys Mar 02 '17 at 14:09
  • 1
    Isn't the issue here not specifying a model at all? The `UserName` and `Password` shown above don't appear to relate to the ones in the class. You'll need to change the parameter type to the name of the class e.g. `public ActionResult Login(LoginModel model)` – G0dsquad Mar 02 '17 at 14:10
  • @LDJ No, but I did the exactly same thing in a different class I have, and It worked properly! What is the problem?! – oneman Mar 02 '17 at 14:10
  • @G0dsquad No, I tried that.. – oneman Mar 02 '17 at 14:15
  • show your Login.cshtml code – Alex Art. Mar 02 '17 at 14:21

3 Answers3

1

Try

public class LoginModel{

[Required(ErrorMessage = "Username cannot be empty")]
public string UserName { get; set; }
[Required(ErrorMessage = "Password cannot be empty")]
public string Password { get; set; }

}

then use it in your action

public ActionResult Login(LoginModel loginModel)
{
.... do stuff here ....

    return View();
}

also make sure that you include

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>

to your view

Please read here: https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

Lys
  • 568
  • 1
  • 4
  • 18
1

If you have this class

public class LoginModel
{
    [Required]
    public string UserName { get; set; }
    [Required]
    public string Password { get; set; }
}

Controller

public ActionResult Login()
{
    return View(new LoginModel());
}

When the view is rendered it uses model (with applied validation attributes) to render unobtrusive validation data attributes. Later those attributes used by jquery.validate.unobtrusive.js to do client side validation.

[HttpPost]
public ActionResult Login(LoginModel model)
{
    if(this.ModelState.IsValid)
    {
          // do something
    }
    else
    {
       return View(model);
    }
}

On post the you have to use the same LoginModel as an argument because it is used by model binder to fill ModelState again by using validation attributes that you decorated your model with.

Alex Art.
  • 8,711
  • 3
  • 29
  • 47
0

I agree with Alex Art's answer, and adding on to his answer you can do this check in the controller:

[HttpPost]
public ActionResult Login(LoginModel model)
{
    if(string.IsNullOrWhiteSpace(model.UserName)
    {
          ModelState.AddModelError("UserName","This field is required!");
          return View(model);
    }

    /* Same can be done for password*/

    /* I am sure once the user has logged in successfully.. you won't want to return the same view, but rather redirect to another action */

    return RedirectToAction("AnotherAction","ControllerName");

}

I hope this helps.

Grizzly
  • 5,873
  • 8
  • 56
  • 109