[英]Display error message in jQuery popup or in alert in MVC 4
我正在使用DataAnnotation进行验证,我想在弹出窗口(对话框/警报)中显示错误消息(数据注释),而不是在视图中显示。...我已经使用此链接实现了代码。
项目模板为“移动”让我知道我是否丢失了一些东西? http://forums.asp.net/t/1738076.aspx/1
Javascript:-
$('#Test').bind('invalid-form.validate', function (form, validator) {
alert('InsideTest');
var $list = $('#errorlist ul:first')
if ($list.length && validator.errorList.length) {
$list.empty();
$.each(validator.errorList, function () {
$("<li />").html(this.message).appendTo(list);
});
$list.dialog({
title: 'Please correct following errors:',
});
}
});
Forgot to add html...
About.cshtml:-
@model WRDSMobile.Models.Test
<div id="errorlist" style="display:none"><ul></ul></div>
@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "Test", id = "Test" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Test</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Age)
@Html.ValidationMessageFor(model => model.Age)
</div>
<input type="submit" value="Create" />
</fieldset>
}
我不确定是否理解您的问题,但是您可以将ajax发布到控制器并返回部分视图。 然后将部分视图加载到您的html元素中,然后在对话框中将其弹出。
[HttpPost]
public ActionResult validate(string firstName,string lastName){
//logic here for validation
return PartialView();
}
$.ajax({
type: "POST",
data: {firstName: nameVal, lastName: lastNameVal },
url: "/myController/validate",
dataType: "html",
success: function (data) {
if (data) {
var dialog = $('<div></div>');
dialog.html(data);
dialog.dialog({
resizable: false,
modal: true,
overflow: false,
maxWidth: 1200,
maxHeight: 600,
width: 1200,
height: 600,
border: 0,
buttons: {
"Cancel": function () { //cancel
$(this).dialog("close");
}
}
});
}
else {
alert("error");
}
}
});
在CSS / JS之后添加以下内容作为参考jquery-ui.css,jquery-1.8.2.min.js,jquery-ui-1.8.24.min.js,jquery.validate.min.js,jquery.validate.unobtrusive。 min.js
$(document).ready(function () {
$('#Test').bind('invalid-form.validate', function (form, validator) {
var $list = $('<ul />')
if (validator.errorList.length) {
$.each(validator.errorList, function (i, entity) {
$("<li />").html(entity.message).appendTo($list);
});
msgbox('Please correct following errors:', $('<div />').append($list));
return false;
}
});
});
function msgbox(_title, _messageHtml) {
$('<div></div>').appendTo('body')
.html(_messageHtml)
.dialog({
modal: true, title: _title, zIndex: 10000, autoOpen: true,
width: 'auto', resizable: false,
buttons: {
Ok: function () {
// $(obj).removeAttr('onclick');
// $(obj).parents('.Parent').remove();
$(this).dialog("close");
}
},
close: function (event, ui) {
$(this).remove();
}
});
};
公共静态void方法(此ModelStateDictionary modelState)
{
StringBuilder errors = new StringBuilder();
try
{
foreach (ModelState model_State in modelState.Values)
foreach (ModelError error in model_State.Errors)
errors.AppendLine(error.ErrorMessage.ToString());
if (errors.Length > 1)
throw new Exception();
}
catch (Exception ex)
{
throw new // your validation helper
}
}
在ajax成功中捕获此错误false并使用此错误错误消息打开jquery对话框注:使用mvc错误处理程序,它将发送ajax success = false的错误消息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.