Div*_*ian 6 validation jquery alert jquery-ui-dialog asp.net-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...
Run Code Online (Sandbox Code Playgroud)
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>
}
Run Code Online (Sandbox Code Playgroud)
我不确定我是否理解你的问题,但你可以 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");
}
}
});
Run Code Online (Sandbox Code Playgroud)