简体   繁体   English

ValidationMessageFor的用法

[英]Usage of ValidationMessageFor

how can i check if the validation error exists for a model property - I am using validationmessagefor method to display the error message. 我如何检查模型属性是否存在验证错误 - 我正在使用validationmessagefor方法来显示错误消息。 How can i just show one error message at a time?. 我怎样才能一次显示一条错误消息? I 一世

Here are model properties i have which m.CommunicationView.MobilePhone.Area m.CommunicationView.MobilePhone.Number 这里有模型属性我有m.CommunicationView.MobilePhone.Area m.CommunicationView.MobilePhone.Number

 <div class="fl">
   @Html.TextBoxFor(m => m.CommunicationView.MobilePhone.Area, new { @id = "personalDetailMobilePhoneAreaInput",  @class="customText wdt80 numericValue",type = "text",maxlength="2" })
   @Html.TextBoxFor(m => m.CommunicationView.MobilePhone.Number, new { @id = "personalDetailMobilePhoneNumberInput",  @class="customText wdt120 mrglft10 phnIE numericValue",type = "text",maxlength="8" })
   @Html.ValidationMessageFor(m => m.CommunicationView.MobilePhone.Area)
   @Html.ValidationMessageFor(m => m.CommunicationView.MobilePhone.Number)
</div>

Just add the ErrorMessage field as part of the [Required] decorator in your model. 只需将ErrorMessage字段添加为模型中[Required]装饰器的一部分即可。 For example for number: 例如,对于数字:

[Required(ErrorMessage = "Number is required")]
[StringLength(10, ErrorMessage = "Number can be no larger than 10 characters")]
public string Number { get; set; }

Sounds like you're talking about client-side validation. 听起来你在谈论客户端验证。 Each field is validated on it's own, so if it has errors, you're going to get an error message. 每个字段都由它自己验证,因此如果它有错误,您将收到一条错误消息。 No consideration is given to whether or not other fields also have error messages. 没有考虑其他字段是否也有错误消息。 From the jQuery Validation docs: 来自jQuery Validation文档:

By default, forms are validated on submit, triggered by the user clicking the submit button or pressing enter when a form input is focused (option onsubmit). 默认情况下,表单在提交时验证,由用户单击提交按钮触发,或在表单输入聚焦时按Enter键(选项onsubmit)。 In addition, once a field was highlighted as being invalid, it is validated whenever the user types something in the field (option onkeyup). 此外,一旦字段被突出显示为无效,只要用户在字段中键入内容(选项onkeyup),就会对其进行验证。 When the user enters something invalid into a valid field, it is also validated when the field loses focus (option onblur). 当用户在无效字段中输入无效内容时,如果该字段失去焦点(选项onblur),也会对其进行验证。 (emphasis mine) (强调我的)

So, as long as the field has not been activated in some way, no error message will appear, but if it has been activate, and has errors, the message will appear. 因此,只要该字段未以某种方式激活,就不会出现任何错误消息,但如果它已被激活且有错误,则会显示该消息。 Again, this is on a field by field basis. 同样,这是逐场的。 However, if the problem is that it's "messing up the UI", you should probably just fix your UI to accomodate the messages. 但是,如果问题是它“弄乱了用户界面”,你应该只修复你的用户界面来容纳消息。 A good UI informs the user about all potential issues, so they don't waste their time in endless submit-and-try-again cycles. 良好的用户界面可以告知用户所有潜在的问题,因此他们不会在无休止的提交和尝试再次循环中浪费时间。

Besides, you'd get the same with server-side validation, as when the view is returned because of model errors, all the errors for all fields will be displayed. 此外,您将获得与服务器端验证相同的功能,因为当模型错误返回视图时,将显示所有字段的所有错误。 Best to just accept this and make your UI more accommodating. 最好接受这个,让你的UI更容易适应。

You need to include the correct jquery libraries that automatically show the error when the textbox / control loses focus. 您需要包含正确的jquery库,它们在文本框/控件失去焦点时自动显示错误。 Put them in your layout page. 将它们放在您的布局页面中。

    @Scripts.Render("~/bundles/jqueryval")
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>  

Make sure those are called after your main call to the jquery library. 确保在主调用jquery库之后调用它们。 Also make sure you have the bundles setup in AppStart/BundleConfig.cs 还要确保在AppStart / BundleConfig.cs中设置了软件包

  bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.validate*"));  

Finally make sure your webconfig has this (Root web.config, not views/web.config) place it in your appSettings node 最后确保你的webconfig有这个(Root web.config,而不是views / web.config)将它放在appSettings节点中

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM