Reputation: 2435
I am curious as to how one would keep the values of in the form when returning a View. So say I had a email and password that must be entered to login and the user entered the email but not the password. Is there any way that when I return the View I can keep the email there so the user just has to enter the password?
This is my if statement:
if (password == "")
{
ViewBag.SpanText = "You must enter a password.";
return View();
}
And these are the values being passed in:
public ActionResult Login(string email, string password)
And this is the .cshtml:
<div class="input-group">
<input type="text" class="form-control" name="email" placeholder="Email">
</div>
<div class="input-group">
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
Upvotes: 0
Views: 737
Reputation: 9499
You need to return the View with the username and password variables. So that razor can render it back.
if (password == "")
{
ViewBag.SpanText = "You must enter a password.";
return View(username, password);
}
Even better, you could create a LoginViewModel with username and password fields and work with the model. You can then use DataAnnotations to put password rules for the password field.
That way, when the user tries to login with a blank password, the page won't event post. Validation rules will kick in and they'll get an error message that you desire.
Upvotes: 1