【问题标题】:MVC on unobtrusive validation fire methodMVC 关于不显眼的验证 fire 方法
【发布时间】:2014-09-12 13:28:41
【问题描述】:

当不显眼的验证返回错误时,我试图在我的 MVC 视图上运行一个方法。我当前的脚本根本不会触发。我试图只在返回错误时运行某个方法。

我尝试过$("#submitButton").click 和类似的方法,但无济于事。

是否只能从内联脚本执行此操作?验证是否有任何触发器?

<fieldset class="step">
                <legend>
                    Contact:
                </legend>
                <p>Let us know how we can contact you with your quote, or anything else we may need from you coming up to your ski trip. </p>
                <div style=" float: left;">
                    <table>
                        <tr style="height: 50px; padding: 5px;">
                            <td style="width: 187px;">
                                Full Name*
                            </td>
                            <td>
                                @Html.DropDownListFor(x => x.Title, Model.Titles, "...........", new { style = "width:70px; padding: 5px;" })

                                @Html.TextBoxFor(x => x.FirstName, new { style = "width:115px; padding: 5px;" })

                                @Html.TextBoxFor(x => x.LastName, new { style = "width:115px; padding: 5px;" })

                            </td>
                        </tr>
                        <tr style="height: 50px; padding: 5px;">
                            <td style="width: 187px;">
                                City*
                            </td>
                            <td>
                                @Html.TextBoxFor(x => x.City, new { style = "width:320px; padding: 5px;" })

                            </td>
                        </tr>
                        <tr style="height: 50px; padding: 5px;">
                            <td style="width: 187px;">
                                Postcode*
                            </td>
                            <td>
                                @Html.TextBoxFor(x => x.Postcode, new { style = "width:320px; padding: 5px;" })

                            </td>
                        </tr>
                        <tr style="height: 50px; padding: 5px;">
                            <td style="width: 187px;">
                                Email*
                            </td>
                            <td>
                                @Html.TextBoxFor(x => x.Email, new { style = "width:320px; padding: 5px;" })

                            </td>
                        </tr>
                        <tr style="height: 50px; padding: 5px;">
                            <td style="width: 187px;">
                                Telephone*
                            </td>
                            <td>
                                @Html.TextBoxFor(x => x.Telephone, new { style = "width:320px; padding: 5px;" })

                            </td>
                        </tr>

                        <tr style="height: 50px; padding: 5px;">
                            <td style="width: 187px;">
                                Preferred Time of Contact
                            </td>
                            <td>
                                @Html.TextBoxFor(x => x.PreferredTime, new { style = "width:200px; padding: 5px;" })

                            </td>
                        </tr>
                    </table>
                </div>

                <input id="submitButton" type="image" alt="Submit" src="/images/global/submit_button.gif" />
            </fieldset>

编辑:刚刚尝试绑定到 invalid-form.validate 方法,但没有成功:

 $(function () {
    $("form").bind("invalid-form.validate", function () {
        alert('invalid!');
    });
});

【问题讨论】:

  • 返回错误是什么意思?你看到了什么,会发生什么?
  • 如果验证失败,那么我希望能够运行一个方法
  • 您可以像这样从 jquery 调用检查: var controls = $("#divid").find('.class'); $.each(controls, function (index, item) { $("#form1").validate().element($(this)); if ($(this).valid) { //可能跳过 } else { //做一些事情,如果需要的话,甚至是一个 ajax 调用 }
  • 点击提交按钮已经触发了验证。当验证失败时,我需要添加额外的功能来集中注意力。我不能使用 onclick,因为页面不允许我提交表单,因为验证,所以我不能运行 onclick

标签: jquery asp.net-mvc unobtrusive-validation


【解决方案1】:
 var controls = $("#divid").find('.class'); 
 $.each(controls, function (index, item) { 
       $("#form1").validate().element($(this)); 
       if ($(this).valid) { 
         //skip it maybe 
       } else { 
            //do something, even an ajax call if need be 
       }

【讨论】:

  • 这就是使用正常验证的方式,而不是不引人注目。默认情况下,MVC 会触发验证
  • 那是检查所需的属性,这是不显眼的方式。这就是我发现捕获任何单个错误并对其进行处理的方法,例如,专注于该字段,$(this).focus(),或显示一条消息或其他任何内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
  • 2016-02-19
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
相关资源
最近更新 更多