Jquery MVC 5与部分视图的引导模式验证不起作用?
我已经断断续续地做了几天了,不能像我预期的那样让它工作。 我看过很多例子,但我一定是误解了什么 我有一个引导模式,它加载了一个局部视图。我想做的是验证 (希望是客户端)在模式中的@ValidationSummary中显示任何错误。最大的问题 我要说的是,当出现错误时,它不是显示在原始模态中,而是基本上加载模态 新页面中的部分视图。验证摘要已正确填充,但没有任何样式 此时不在模态中 *注意:目前我没有使用AJAX加载模式。我最终会得到的,但我不舒服 有了AJAX,我想先让它工作起来,然后再回来用AJAX进行重构 \u Layout.cshtml-我发现一个例子说我需要在加载Modal时加载JS.unobtrusive。我想 我这样做是正确的,但我可能遗漏了一些东西Jquery MVC 5与部分视图的引导模式验证不起作用?,jquery,asp.net-mvc,twitter-bootstrap,asp.net-mvc-5,Jquery,Asp.net Mvc,Twitter Bootstrap,Asp.net Mvc 5,我已经断断续续地做了几天了,不能像我预期的那样让它工作。 我看过很多例子,但我一定是误解了什么 我有一个引导模式,它加载了一个局部视图。我想做的是验证 (希望是客户端)在模式中的@ValidationSummary中显示任何错误。最大的问题 我要说的是,当出现错误时,它不是显示在原始模态中,而是基本上加载模态 新页面中的部分视图。验证摘要已正确填充,但没有任何样式 此时不在模态中 *注意:目前我没有使用AJAX加载模式。我最终会得到的,但我不舒服 有了AJAX,我想先让它工作起来,然后再回来用A
<div id="modal-container" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
<script type="text/javascript">
//**** Bootstrap Modal - used with Partial Views ***********************
$(function () {
// Initalize modal dialog
// attach modal-container bootstrap attributes to links with .modal-link class.
// when a link is clicked with these attributes, bootstrap will display the href content in a modal dialog.
$('body').on('click', '.modal-link', function (e) {
e.preventDefault();
$(this).attr('data-target', '#modal-container');
$(this).attr('data-toggle', 'modal');
//load the unobtrusive JS code
$jQval.unobtrusive.parse($modal-container);
var $form = $modal-container.find("form");
$.validator.unobtrusive.parse($form);
});
// Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears
$('body').on('click', '.modal-close-btn', function () {
$('#modal-container').modal('hide');
});
//clear modal cache, so that new content can be loaded
$('#modal-container').on('hidden.bs.modal', function () {
$(this).removeData('bs.modal');
});
$('#CancelModal').on('click', function () {
return false;
});
});
Bundle.config
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery.validate.js",
"~/Scripts/jquery.validate.unobtrusive.js"));
您不能传回局部视图,因为它仅返回局部视图-而不是视图+模态内部加载的局部视图。所以你现在看到的是正确的 您需要使用AJAX传回JSON响应,以便在模态的部分视图中进行验证 如果模型状态无效,则返回模型状态的键值对。一个很好的开始示例如下: 发送回视图后,可以使用jquery将模型错误(如果有)附加到验证摘要区域 编辑: 如有要求,举例如下: 控制器
public class HomeController : BaseController
{
[HttpPost]
public ActionResult Edit(EditViewModel vm)
{
if(ModelState.IsValid)
{
//do stuff
return Json(new
{
status = "success"
//return values if needed
}
}
return Json(new
{
status = "failure",
formErrors = ModelState.Select(kvp => new { key = kvp.Key, errors = kvp.Value.Errors.Select(e => e.ErrorMessage)})});
}
}
}
查看
@using (Ajax.BeginForm("Edit", new AjaxOptions { OnSuccess = "onChangeSuccess"}))
{
//Modal header, body, footer
//Make sure your form fields actually contain their Razor validation fields
}
JQuery
function onChangeSuccess(data) {
if (data.status === "success") {
$("#modalcontent").modal('hide');
$("#message-area").html(data.view);
}
$.each(data.formErrors, function() {
$("[data-valmsg-for=" + this.key + "]").html(this.errors.join());
});
}
谢谢我来试试。回答得很好!这对我很有帮助,因为我们在局部视图中有一个ajax表单,数据valmsg应该是什么?
public class HomeController : BaseController
{
[HttpPost]
public ActionResult Edit(EditViewModel vm)
{
if(ModelState.IsValid)
{
//do stuff
return Json(new
{
status = "success"
//return values if needed
}
}
return Json(new
{
status = "failure",
formErrors = ModelState.Select(kvp => new { key = kvp.Key, errors = kvp.Value.Errors.Select(e => e.ErrorMessage)})});
}
}
}
@using (Ajax.BeginForm("Edit", new AjaxOptions { OnSuccess = "onChangeSuccess"}))
{
//Modal header, body, footer
//Make sure your form fields actually contain their Razor validation fields
}
function onChangeSuccess(data) {
if (data.status === "success") {
$("#modalcontent").modal('hide');
$("#message-area").html(data.view);
}
$.each(data.formErrors, function() {
$("[data-valmsg-for=" + this.key + "]").html(this.errors.join());
});
}