在jQuery弹出窗口或MVC4中的警报中显示错误消息

在jQuery弹出窗口或MVC4中的警报中显示错误消息,jquery,validation,asp.net-mvc-4,jquery-ui-dialog,alert,Jquery,Validation,Asp.net Mvc 4,Jquery Ui Dialog,Alert,我正在使用DataAnnotation进行验证,我想在弹出窗口(对话框/警报)中显示错误消息(数据批注),而不是在视图中显示它。。。。 我已经用这个链接实现了代码 项目模板是移动的 如果我遗漏了什么,请告诉我?? Javascript:- $('#Test').bind('invalid-form.validate', function (form, validator) { alert('InsideTest'); var $list = $('#errorlist

我正在使用DataAnnotation进行验证,我想在弹出窗口(对话框/警报)中显示错误消息(数据批注),而不是在视图中显示它。。。。 我已经用这个链接实现了代码

项目模板是移动的 如果我遗漏了什么,请告诉我??

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...    
$('#Test').bind('invalid-form.validate',函数(form,validator){
警报(“内部测试”);
var$list=$(“#errorlist ul:first”)
if($list.length&&validator.errorList.length){
$list.empty();
$.each(validator.errorList,函数(){
$(“
  • ”)html(this.message).appendTo(list); }); $list.dialog({ 标题:“请更正以下错误:”, }); } });
    忘记添加html。。。
  • 关于.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>
    }
    
    @model WRDSMobile.Models.Test
    
      @使用(Html.BeginForm(null,null,FormMethod.Post,new{name=“Test”,id=“Test”})) { @Html.ValidationSummary(true) 试验 @LabelFor(model=>model.Name) @EditorFor(model=>model.Name) @Html.ValidationMessageFor(model=>model.Name) @LabelFor(model=>model.Age) @EditorFor(model=>model.Age) @Html.ValidationMessageFor(model=>model.Age) }
      我不确定是否理解您的问题,但您可以将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");
      
                      }
                  }
              });
      
      [HttpPost]
      公共操作结果验证(字符串名、字符串名){
      //这里的逻辑用于验证
      返回PartialView();
      }
      $.ajax({
      类型:“POST”,
      数据:{firstName:nameVal,lastName:lastNameVal},
      url:“/myController/validate”,
      数据类型:“html”,
      成功:功能(数据){
      如果(数据){
      变量对话框=$('');
      html(数据);
      对话({
      可调整大小:false,
      莫代尔:是的,
      溢出:错误,
      最大宽度:1200,
      最大高度:600,
      宽度:1200,
      身高:600,
      边界:0,
      按钮:{
      “Cancel”:函数(){//Cancel
      $(此).dialog(“关闭”);
      }
      }
      });
      }
      否则{
      警报(“错误”);
      }
      }
      });
      
      公共静态无效方法(此ModelStateDictionary modelState)

      }

      在ajax Suces false open jquery对话框中捕获此错误,并显示此错误消息
      注意:使用mvc错误处理程序,它将发送错误消息,其中ajax success=false

      添加以下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();
                              }
                          });
          };
      
      $(文档).ready(函数(){
      $('#Test').bind('invalid-form.validate',函数(form,validator){
      var$list=$(“
        ”) if(validator.errorList.length){ $.each(validator.errorList,函数(i,实体){ $(“
      • ”)html(entity.message).appendTo($list); }); msgbox('请更正以下错误:',$('')。追加($list)); 返回false; } }); }); 函数msgbox(_title,_messageHtml){ $('').appendTo('正文') .html(_messageHtml) .对话({ 模态:真,标题:\标题,zIndex:10000,自动打开:真, 宽度:“自动”,可调整大小:false, 按钮:{ Ok:函数(){ //$(obj).removeAttr('onclick'); //$(obj).parents('.Parent').remove(); $(此).dialog(“关闭”); } }, 关闭:功能(事件、用户界面){ $(this.remove(); } }); };
      忘记添加html…没有问题,您可以编辑和更新问题:)您的问题是什么?此代码是否遇到问题?我没有收到任何错误。。表单直接发布我需要在PoputHanks中显示数据注释(客户端)错误,目前正在使用jquery验证,但我正在检查它是否可以帮助其他人,错误消息也会出现在弹出窗口中的html上
      $(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();
                              }
                          });
          };