Modelstate.addmodelerror 未在 PartialView 中显示
Modelstate.addmodelerror not showing in PartialView
addmodelerror 在视图中起作用,但在部分视图中不起作用。
我的代码:
在视图 Login.cshtml :
中调用的部分视图“_login”
@model MODEL.Identity.LoginViewModel
@using (Ajax.BeginForm("Login", "MyAccount", new AjaxOptions { HttpMethod = "POST"}, new { role = "form" }))
{
@Html.AntiForgeryToken()
<div class="main-container">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="container">............
显示了电子邮件和密码的错误消息,但我的没有 ModelState.AddModelError("", "Nom d'utilisateur ou mot de passe non valide.");
代码实战:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
ApplicationDbContext context = new ApplicationDbContext();
var u = await UserManager.FindByEmailAsync(model.Email);
bool passhash = false;
if (u != null)
{
passhash = await UserManager.CheckPasswordAsync(u, model.Password);
}
if (u != null && passhash)
{
await SignInAsync(u, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Nom d'utilisateur ou mot de passe non valide.");
}
}
return PartialView("~/views/MyAccount/_login.cshtml",model);
}
帮忙谢谢
已解决,但我不知道这是否是好的做法?
这是我的代码
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<string> Login(LoginViewModel model )
{
var u = await UserManager.FindByEmailAsync(model.Email);
bool passhash = false;
if (u != null)
{
passhash = await UserManager.CheckPasswordAsync(u, model.Password);
}
if (u != null && passhash)
{
await SignInAsync(u, model.RememberMe);
return ("ok");
}
else
return ("Nom d'utilisateur ou mot de passe non valide.");
}
以及我的观点或部分观点
@model MODEL.Identity.LoginViewModel
<script type="text/javascript">
function OnSuccess(response) {
if (response != "ok")
{
document.getElementById("errormessage").innerHTML = response
}
else
{
window.location.href = "/Home/Index/";
}
}
</script>
@using (Ajax.BeginForm("Login", "MyAccount", new AjaxOptions { OnSuccess = "OnSuccess" }, new { @class = "form-horizontal", role = "form" }))
{
<div id="errormessage" class='field-validation-error text-danger' style=" display: block;"></div>
@Html.AntiForgeryToken()
<div class="main-container">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="container">............
如果你有任何其他想法,post它
addmodelerror 在视图中起作用,但在部分视图中不起作用。
我的代码: 在视图 Login.cshtml :
中调用的部分视图“_login”@model MODEL.Identity.LoginViewModel
@using (Ajax.BeginForm("Login", "MyAccount", new AjaxOptions { HttpMethod = "POST"}, new { role = "form" }))
{
@Html.AntiForgeryToken()
<div class="main-container">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="container">............
显示了电子邮件和密码的错误消息,但我的没有 ModelState.AddModelError("", "Nom d'utilisateur ou mot de passe non valide."); 代码实战:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
ApplicationDbContext context = new ApplicationDbContext();
var u = await UserManager.FindByEmailAsync(model.Email);
bool passhash = false;
if (u != null)
{
passhash = await UserManager.CheckPasswordAsync(u, model.Password);
}
if (u != null && passhash)
{
await SignInAsync(u, model.RememberMe);
return RedirectToLocal(returnUrl);
}
else
{
ModelState.AddModelError("", "Nom d'utilisateur ou mot de passe non valide.");
}
}
return PartialView("~/views/MyAccount/_login.cshtml",model);
}
帮忙谢谢
已解决,但我不知道这是否是好的做法? 这是我的代码
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<string> Login(LoginViewModel model )
{
var u = await UserManager.FindByEmailAsync(model.Email);
bool passhash = false;
if (u != null)
{
passhash = await UserManager.CheckPasswordAsync(u, model.Password);
}
if (u != null && passhash)
{
await SignInAsync(u, model.RememberMe);
return ("ok");
}
else
return ("Nom d'utilisateur ou mot de passe non valide.");
}
以及我的观点或部分观点
@model MODEL.Identity.LoginViewModel
<script type="text/javascript">
function OnSuccess(response) {
if (response != "ok")
{
document.getElementById("errormessage").innerHTML = response
}
else
{
window.location.href = "/Home/Index/";
}
}
</script>
@using (Ajax.BeginForm("Login", "MyAccount", new AjaxOptions { OnSuccess = "OnSuccess" }, new { @class = "form-horizontal", role = "form" }))
{
<div id="errormessage" class='field-validation-error text-danger' style=" display: block;"></div>
@Html.AntiForgeryToken()
<div class="main-container">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="container">............
如果你有任何其他想法,post它