C# Modelstate.addmodelerror未显示在PartialView中
addmodelerror正在使用fr视图,但不在局部视图中 我的代码: 在view login.cshtml中调用的部分视图“\u login”: @model.Identity.LoginViewModelC# Modelstate.addmodelerror未显示在PartialView中,c#,asp.net-mvc-5,asp.net-identity-2,C#,Asp.net Mvc 5,Asp.net Identity 2,addmodelerror正在使用fr视图,但不在局部视图中 我的代码: 在view login.cshtml中调用的部分视图“\u login”: @model.Identity.LoginViewModel @using (Ajax.BeginForm("Login", "MyAccount", new AjaxOptions { HttpMethod = "POST"}, new { role = "form" })) { @Html.AntiForgeryToken()
@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">............
@使用(Ajax.beginnform(“Login”、“MyAccount”、新的AjaxOptions{HttpMethod=“POST”}、新的{role=“form”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
............
将显示电子邮件和密码的错误消息,但不显示my ModelState.AddModelError(“,“Nom d'Usitateur 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]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginView模型,字符串返回URL)
{
if(ModelState.IsValid)
{
ApplicationDbContext上下文=新的ApplicationDbContext();
var u=await UserManager.findbyemailsync(model.Email);
bool passhash=false;
如果(u!=null)
{
passhash=await UserManager.CheckPasswordAsync(u,model.Password);
}
if(u!=null&&passhash)
{
等待信号同步(u,型号记忆);
返回重定向到本地(returnUrl);
}
其他的
{
ModelState.AddModelError(“,“使用名称或无效通行证”);
}
}
返回PartialView(“~/views/MyAccount/\u 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.");
}
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginViewModel模型)
{
var u=await UserManager.findbyemailsync(model.Email);
bool passhash=false;
如果(u!=null)
{
passhash=await UserManager.CheckPasswordAsync(u,model.Password);
}
if(u!=null&&passhash)
{
等待信号同步(u,型号记忆);
返回(“ok”);
}
其他的
报税表(“使用名称或非有效通行证”);
}
我的观点还是局部观点
@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">............
@model.Identity.LoginViewModel
函数OnSuccess(响应){
如果(响应!=“确定”)
{
document.getElementById(“errormessage”).innerHTML=response
}
其他的
{
window.location.href=“/Home/Index/”;
}
}
@使用(Ajax.BeginForm(“Login”、“MyAccount”、新的AjaxOptions{OnSuccess=“OnSuccess”}、新的{@class=“form horizontal”、role=“form”}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
............
如果你有任何其他想法,发布它你的元素是什么?使用(Ajax.BeginForm(“Login”,“MyAccount”,new AjaxOptions{HttpMethod=“post”,UpdateTargetId=“val123”},new{role=“form”}),我的意思是@model model.Identity.LoginViewModel使用(Ajax.BeginForm(“Login”,“MyAccount”,新的AjaxOptions{HttpMethod=“POST”},新的{role=“form”})){@Html.AntiForgeryToken()Html.ValidationSummary(true,”,新的{class=“text danger”})请编辑问题(不在注释中)。您显示的代码将显示由
ModelState.AddModelError()添加的错误
如果它命中那一行。但是你的代码毫无意义-你在控制器中有一个返回重定向到操作(“索引”,“主页”);
,但是ajax调用从不重定向(ajax的全部目的是停留在同一页上。摆脱ajax.BeginForm()
并使用Html.BeginForm()
进行正常提交。您有一个从未使用过的字符串returnUrl
参数!正常行为是将用户返回到导航到的页面(即返回URL
-而不是索引
视图)