El campo obligatorio de MVC no funciona
Frecuentes
Visto 7,571 veces
0
Title says it all, can anyone spot what I'm doing wrong. I've tried moving around my HTMlValidation Summary and a bunch of other things. I feel like it may have something to with the views I am returning from my Controller Class.
-- Model
public class Login
{
[Required(ErrorMessage = "First Name is required")]
[Display(Name = "First Namex")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Password is required")]
[DataType(DataType.Password)]
[Display(Name = "Passwordx")]
public string Password { get; set; }
}
-- Controller
[HttpPost]
public ActionResult Login(string FirstName, string Password)
{
if (ModelState.IsValid)
{
bool validLogin = new UserBAL().ValidateUser(FirstName, Password);
{
if (validLogin == true)
{
return RedirectToAction("Index", "Invoice");
}
else
{
return RedirectToAction("Index");
// ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
}
return View();
}
--View
@using (Html.BeginForm("Login", "Home"))
{ @Html.ValidationSummary(true)
<div>
<fieldset>
<legend>Login</legend>
<div class ="fields">
@Html.LabelFor(u => u.FirstName)
</div>
@Html.TextBoxFor(u => u.FirstName)
@Html.ValidationMessageFor(u => u.FirstName) <br />
<div class ="fields">
@Html.LabelFor(u => u.Password)
</div>
@Html.PasswordFor(u => u.Password) <br />
<input type="submit" value="Log In" />
</fieldset>
</div>
}
[HttpPost]
public ActionResult Login(EIAS.Models.Login login)
{
if (ModelState.IsValid)
{
bool validLogin = new UserBAL().ValidateUser(login);
{
if (validLogin == true)
{
return RedirectToAction("Index", "Invoice");
}
else
{
return RedirectToAction ("Index");
// ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
}
return View();
}
3 Respuestas
5
MVC in if (ModelState.IsValid)
validate the model, and you don't receive the model in your action:
public ActionResult Login(string FirstName, string Password)
change the parameters of the action to:
public ActionResult Login(Login model)
and, for validate in the client, check if:
- if you include jquery validate plugin (js)
- check your web.config, keys
ClientValidationEnabled
yUnobtrusiveJavaScriptEnabled
ir are in true.
Respondido el 10 de Septiembre de 13 a las 03:09
0
You have to take the Model as the parameter to your Login action
public ActionResult Login()
{
// This will return the view with the form that you have above
return View();
}
[HttpPost]
public ActionResult Login(Login login)
{
// this is what your form will post too
if (ModelState.IsValid)
{
bool validLogin = new UserBAL().ValidateUser(login.FirstName, login.Password);
{
if (validLogin == true)
{
return RedirectToAction("Index", "Invoice");
}
else
{
return RedirectToAction("Index");
// ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
}
return View();
}
In the View just have
@using (Html.BeginForm())
Here is a link about MVC Razor Forms: http://blog.michaelckennedy.net/2012/01/20/building-asp-net-mvc-forms-with-razor/
Trata eso.
Respondido el 09 de Septiembre de 13 a las 22:09
0
The problem was with my Views and which views I was returning. I was trying to use my Index view for validation, I hadn't created a Login View, so once I did that and added my validation to the Login View it worked. So in my question, Return View() wasn't really returning anything
Respondido el 10 de Septiembre de 13 a las 15:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas asp.net-mvc validation required or haz tu propia pregunta.
ok, then How can I pass the textbox values to my business layer function since it takes 2 arguements, firstname and password - C
yes I made it ValidateUser(login) and passed it through the layers like that before being able to use login."FIeld" - C