设置重点放在ASP.NET MVC模型误差 [英] Set focus on on ASP.NET MVC model errors

查看:90
本文介绍了设置重点放在ASP.NET MVC模型误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,做了一些验证检查,并增加了错误的ModelState:

I've got a model that does some validation checking and adds the errors to ModelState:

ViewData.ModelState.AddModelError("mycontrol", "message")

他们显示在观看侧罚款,但有没有办法将焦点设置到对应于验证消息的控制?眼下,页面刷新,并停留在页面的顶部,因此,如果错误是在页的末尾,这不是很明显发生了什么用户。

They display fine on the view side, but is there a way to set the focus to the control that corresponds to the validation message? Right now, the page refreshes and stays at the top of the page, so if the error is towards the end of the page, it's not obvious to the user what happened.

注:另一种解决办法是的ValidationSummary显示在页面顶部的错误列表,但我从来没有能够得到它显示任何内容。通过ValidationMessage显示我的所有错误。

Note: Another solution would be for ValidationSummary to show the list of errors at the top of the page, but I've never been able to get it to display anything. All my errors are displayed via ValidationMessage.

编辑:我发现我的问题的ValidationSummary。我的标记是:

I found my problem with ValidationSummary. The markup I had was:

<% Html.ValidationSummary()%>

这应该是:

<%=Html.ValidationSummary()%>

我还是想知道如何捕捉到字段但是错误。

I'd still like to know how to snap to the field with the error however.

推荐答案

一些jQuery善良滚动到一个错误的第一个输入。棘手的一点是,你必须在你调用焦点()作为一个jQuery对象对焦点()方法来获取潜在的DOM元素触发而不是给出焦点元素的焦点事件。

Some jquery goodness to scroll to the first input with an error. The tricky bit is that you have to get the underlying DOM element BEFORE you invoke focus() as the focus() method on a jQuery object fires the focus event instead of giving focus to the element.

<script type='text/javascript'>
    $(document).ready( function()
    {
       var input = $('.input-validation-error:first');

       if(input)
       {
           input.focus();
       }
    });
</script>

这篇关于设置重点放在ASP.NET MVC模型误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆