MVC4 - 不显眼的审定出发太早 [英] MVC4 - Unobtrusive validation starting too early

查看:138
本文介绍了MVC4 - 不显眼的审定出发太早的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用不引人注目的验证我的网站,该网站是一个ASP.Net应用MVC4一个HTML表单。从我的观点的形式很简单,如下所示

  @using(Html.BeginForm())
{
  @:清单@ ViewBag.itemNumber
  @ Html.TextBoxFor(型号=> model.Name)
  @ Html.TextBoxFor(型号=> model.Location)
  @:LT;输入类型=提交值=保存/>
}

我的控制器如下图所示。

 公众的ActionResult测试()
{
  字符串itemNumber = Request.params [项目];
  ViewBag.itemNumber = itemNumber;
  返回查看();
}

和我的模型是

 公共类项目
{
 [StringLength(10的ErrorMessage =的名字应该是长度ATLEAST 3个字符,MinimumLength = 3)]
 公共字符串名称{;设置;}
 公共字符串位置{获取;设置;}
}

我遇到的问题是,只要在页面加载,甚至我的第一个表之前,不显眼的审定踢提交。 Name的文本框,只要我启动,因为最小长度验证打字变为红色。我已经成功,并没有在同一网站上的其他网页这一问题不显眼的使用验证。我怀疑验证早期这个特定页面上踢因为在我的控制器中的code以检索从URL GET参数的值,并将其设置在Viewbag的。是我的假设是正确的?如果是,我怎么能prevent验证从这个页面上太早开始?

编辑:我有我的相同顺序页面下面的JavaScript库下方

所指定

 <脚本SRC =〜/脚本/ jQuery的-1.8.2.min.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =〜/脚本/ jQuery的-UI-1.9.0.min.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =〜/脚本/ jQuery.tmpl.min.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =〜/脚本/ jquery.validate.min.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =〜/脚本/ jquery.validate.unobtrusive.min.js类型=文/ JavaScript的>< / SCRIPT>
<脚本SRC =〜/脚本/ jquery.unobtrusive-ajax.min.js类型=文/ JavaScript的>< / SCRIPT>


解决方案

您所描述的是默认的行为!字段也进行验证的onkeyup为默认值。这并未与必填字段发生仅仅是因为当钥匙高达文本框不是空了!因此,也许你感到困惑的必填字段例的行为。
反正你可以禁用KEYUP验证。看到这里插件的充分的选择可以改变: http://docs.jquery.com /插件/确认/验证#toptions

I have a HTML form that uses unobtrusive validation in my website which is an ASP.Net MVC4 application. The form from my view is simple and is shown below

@Using (Html.BeginForm())
{
  @: Listing for @ViewBag.itemNumber
  @Html.TextBoxFor(model=>model.Name)
  @Html.TextBoxFor(model=>model.Location)
  @:<input type="submit" value="Save" />
}

My controller is as shown below

public ActionResult test()
{
  string itemNumber = Request.params["item"];
  ViewBag.itemNumber = itemNumber;
  return View();
}

and my model is

public class item
{
 [StringLength(10, ErrorMessage = "The Name should be atleast 3 characters in length", MinimumLength = 3)]
 public string Name {get;set;}
 public string Location {get;set;}
}

The problem I am having is that the unobtrusive validation kicks in as soon as the page loads even before my first form submit. The textbox for Name turns red as soon as I start typing because of the minimum length validation. I have used unobtrusive validation successfully and without this issue on other pages of the same website. I suspect that the validation is kicking in early on this particular page because of the code in my controller which retrieves a value from the URL GET parameters and sets it in the Viewbag. Is my assumption correct? If it is, how can I prevent the validation from starting too early on this page?

EDIT: I have the following javascript libraries on my page in the same order as specified below

<script src="~/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-ui-1.9.0.min.js" type="text/javascript"></script>
<script src="~/Scripts/jQuery.tmpl.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>

解决方案

The one you described is the default behaviour! Fields are validated also onKeyUp as default. This doesn happens with required fields simply because when the key is up the textbox is not empty anymore! So maybe you are confused with the behaviour of the required field case. Anyway you can disable the keyUp validation. See here the full options of the plugin you can change : http://docs.jquery.com/Plugins/Validation/validate#toptions

这篇关于MVC4 - 不显眼的审定出发太早的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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