.NET MVC客户端验证没有出现财产信息时,值无效 [英] .NET MVC client side validation not showing message for property when value invalid

查看:98
本文介绍了.NET MVC客户端验证没有出现财产信息时,值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读关于这个题材很多帖子,但我仍然有我的问题作战。我有一个具有14属性和5复杂类型与自定义模板编辑器的形式。客户端验证适用于所有属性和自定义模板编辑器,但低于财产。如果我输入文字,它会删除没有消息的文本框中的全部内容。但是如果我输入2,我收到错误消息,它必须是一个数字。有没有人经历过这种行为?这发生在我的其他形式的一些为好。

作为一个方面说明:如果客户端验证可空INT范围是多少?好像它不,除非我添加了一个范围验证。谢谢。

更新
这是实际发生的事情为我所有的诠释?性能

型号

  [显示(名称=终身)]
公众诠释?生活{搞定;组; }

查看

  @using(Html.BeginForm(EditPerson,维护,FormMethod.Post,新{@class =操作型}))
{
    @ Html.AntiForgeryToken()
    @ Html.HiddenFor(型号=> model.PersonId)
    @ Html.Hidden(ModifiedByUser.Identity.Name)    < D​​IV CLASS =形横>
        @ Html.ValidationSummary(假,新{@class =TEXT-危险})
      < D​​IV CLASS =COL-MD-4>
        < D​​IV CLASS =排人场>
          < D​​IV CLASS =COL-MD-6字段名>
             @ Html.LabelFor(型号=> model.Life)
          < / DIV>
          < D​​IV CLASS =COL-MD-6>
             @ Html.EditorFor(型号=> model.Life,新{htmlAttributes = {新@class =到中心routebox}})
             @ Html.ValidationMessageFor(型号=> model.Life,新{@class =TEXT-危险})
          < / DIV>
        < / DIV>
      < / DIV>
    < / DIV>
} @section脚本{
@ Scripts.Render(〜/包/ jqueryval)


解决方案

如果您使用的是现代的浏览器,该问题可能是使用 @ Html.EditorFor()的默认情况下这将增加 TYPE =号来输入,因此会产生浏览器实现了一些控制。我只测试了使用Chrome,但现在看来,如果输入的文本不是有效的号码,它和prevents的属性被设置浏览器拦截。现在,当你回发,人寿为空,因此有效的,当你回来时认为,的ModelState 值为null解释为什么它似乎的

没有消息删除文本框中的全部内容

您可以将其更改为 @ Html.TextBoxFor测试这个(M = GT; m.Life)这会使键入=TEXT 。现在,当您输入文字,并尝试提交,不显眼的审定会prevent提交和显示的外地生活必须是一个数字的。至于验证的范围,如果你不添加适当的属性属性(如 [范围(0,int.MaxValue)] ),那么就没有什么了 jquery.validate.unobtrusive 来测试,以便它回发,但其验证的服务器上,当你回来的观点,会的的值显示验证错误等999999999999999999999999999999999999999999'是无效的

I have read many posts on this subject matter, but I am still battling with my issue. I have a form that has a 14 properties and 5 complex types with custom template editors. Client side validation works for all properties and custom template editors, but the property below. If I enter text, it erases the entire contents of the text box with no message. However if I enter 2., I receive the error message saying it must be an number. Has anyone experienced this behavior? This happens on a few of my other forms as well.

As a side note: Should client side validate the nullable int range? Seems like it does not unless I add a range validator. Thank you.

UPDATE This is actually happening for all my int? properties

Model

[Display(Name = "Lifetime")]
public int? Life { get; set; }

View

@using (Html.BeginForm("EditPerson", "Maintenance", FormMethod.Post, new { @class = "operation-form" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.PersonId)
    @Html.Hidden("ModifiedBy", User.Identity.Name)

    <div class="form-horizontal">
        @Html.ValidationSummary(false, "", new { @class = "text-danger" })
      <div class="col-md-4">
        <div class="row person-field">
          <div class="col-md-6 field-name">
             @Html.LabelFor(model => model.Life)
          </div>
          <div class="col-md-6">
             @Html.EditorFor(model => model.Life, new { htmlAttributes = new { @class = "to-center routebox" } })
             @Html.ValidationMessageFor(model => model.Life, "", new { @class = "text-danger" })
          </div>
        </div>
      </div>
    </div>
}

 @section Scripts {
@Scripts.Render("~/bundles/jqueryval")

解决方案

If your using a modern browser, the problem may be the use of @Html.EditorFor() By default this will add type="number" to the input and as a result will generate the browsers implementation of a number control. I have only tested this using Chrome, but it appears that if you enter text that not a valid number, the the browser intercepts it and prevents the value attribute being set. Now when you post back, Life is null and therefore valid and when you return the view, the ModelState value is null explaining why it appears to "erase the entire contents of the text box with no message".

You can test this by changing it to @Html.TextBoxFor(m => m.Life) which will render type="text". Now when you enter text and try to submit, unobtrusive validation will prevent submission and display "The field Life must be a number". As for validating the range, if you do not add an appropriate attribute to the property (e.g [Range(0, int.MaxValue)]) then there is nothing for jquery.validate.unobtrusive to test so it posts back, however its validated on the server and when you return the view, a validation error will be displayed such as "The value '999999999999999999999999999999999999999999' is invalid".

这篇关于.NET MVC客户端验证没有出现财产信息时,值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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