Asp.net mvc MVC必填字段不工作
标题说明了一切,谁能看出我做错了什么。我已经试着在我的HTMlValidation摘要和其他一些东西周围移动。我觉得这可能与我从控制器类返回的视图有关Asp.net mvc MVC必填字段不工作,asp.net-mvc,validation,required,Asp.net Mvc,Validation,Required,标题说明了一切,谁能看出我做错了什么。我已经试着在我的HTMlValidation摘要和其他一些东西周围移动。我觉得这可能与我从控制器类返回的视图有关 -- Model public class Login { [Required(ErrorMessage = "First Name is required")] [Display(Name = "First Namex")] public string FirstName { get; set; } [Req
-- 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>
您必须将模型作为登录操作的参数
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();
}
在视图中,只有
@using (Html.BeginForm())
以下是有关MVC Razor表单的链接:
试试看。您必须将模型作为登录操作的参数
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();
}
在视图中,只有
@using (Html.BeginForm())
以下是有关MVC Razor表单的链接:
试一试。如果(ModelState.IsValid)验证模型,并且您的操作中没有收到模型,请执行以下操作:
public ActionResult Login(string FirstName, string Password)
将操作的参数更改为:
public ActionResult Login(Login model)
对于在客户端中验证,请检查是否:
- 如果包含jquery验证插件(js)
- 检查您的web.config,键
和ClientValidationEnabled
ir为trueUnobtrusiveJavaScriptEnabled
public ActionResult Login(string FirstName, string Password)
将操作的参数更改为:
public ActionResult Login(Login model)
对于在客户端中验证,请检查是否:
- 如果包含jquery验证插件(js)
- 检查您的web.config,键
和ClientValidationEnabled
ir为trueUnobtrusiveJavaScriptEnabled
问题在于我的视图和返回的视图。我试图使用我的索引视图进行验证,但我没有创建登录视图,所以一旦我创建了,并将验证添加到登录视图中,它就可以工作了。所以在我的问题中,Return View()实际上并没有返回任何内容问题在于我的视图和返回的视图。我试图使用我的索引视图进行验证,但我没有创建登录视图,所以一旦我创建了,并将验证添加到登录视图中,它就可以工作了。所以在我的问题中,Return View()实际上没有返回任何内容ok,那么我如何才能将文本框值传递给我的业务层函数,因为它需要两个参数,firstname和password是的,我将其设置为ValidateUser(login),并在能够使用login之前通过类似的层传递。“FIeld”ok,那么我如何才能将文本框值传递给我的业务层函数,因为它需要两个参数,firstname和password是的,我将其设置为ValidateUser(login),并在能够使用login之前通过类似的层传递它。“FIeld”