本文介绍了如何进行模态验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个返回登录视图的模式.我想检查用户是否不存在,返回带有一些错误的视图.我尝试使用
I have a modal that returns login view . I want to check if user does not exist return the view with some error . I tried using
但是模式关闭并打开视图.这是我的代码:
But the modal close and view is opened.this is my code:
public IActionResult Login(LoginViewModel login)
{
if (!ModelState.IsValid)
return View();
var user = _userServies.getUserByEmailandPass(login.Email, login.Password);
if (user == null)
{
ModelState.AddModelError("Email","email or password is wrong");
return view();
}
return Redirect("/");
}
推荐答案
使用 jQuery
发送 ajax
并动态执行模式操作.
Use jQuery
to send ajax
and perform modal actions dynamically.
这是控制器的代码.
[HttpPost]
public IActionResult Login(LoginViewModel login)
{
if (ModelState.IsValid)
{
//var user = _userServies.getUserByEmailandPass(login.Email, login.Password);
//if (user == null)
if (login.Email.Equals(login.Password))
{
ModelState.AddModelError("Email", "email or password is wrong");
}
}else
ModelState.AddModelError("Email", "email or password invalid");
return PartialView("_LoginModalPartial", login);
}
以下是 _LoginModalPartial.cshtml
的代码.
@model LoginViewModel
@{
Layout = "_Layout";
}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#login">
Login
</button>
<div id="modal-placeholder">
<!-- Modal -->
<div class="modal fade" id="login" tabindex="-1" role="dialog" aria-labelledby="addContactLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addContactLabel">Login</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form asp-action="Login">
<input name="IsValid" type="hidden" value="@ViewData.ModelState.IsValid.ToString()" />
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-save="modal">Login</button>
</div>
</div>
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script>
$(function () {
$('button[data-save="modal"]').click(function (event) {
var form = $(this).parents('.modal').find('form');
var actionUrl = form.attr('action');
var dataToSend = form.serialize();
$.post(actionUrl, dataToSend).done(function (data) {
var placeholderElement = $('#modal-placeholder');
var newBody = $('.modal-body', data);
// find IsValid input field and check it's value
// if it's valid then hide modal window
var isValid = newBody.find('[name="IsValid"]').val() == 'True';
if (isValid) {
placeholderElement.find('.modal').modal('hide');
location.href = "/";
} else {
placeholderElement.find('.modal-body').replaceWith(newBody);
}
});
});
});
</script>
这篇关于如何进行模态验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!