Kendo ui - how to tie validation to mvc model attributes

社会主义新天地 提交于 2019-12-11 01:51:15

问题


From reading the posts in this thread - and being unable to post the question there for some bizarre reason :( I will ask it here in hope of getting a solution

Am I write in saying that I have to do validation like below..

  1. I add the html5 attribute (data-required-msg/validationMessage) to the textbox and the required attribute as well..
  2. I make a span for the invalid msg and tie it to the field with the "data-for" attribute. The message "Please enter name" should appear in this span then.

Questions

  1. Is this the only way to work with this?
  2. Is there no way for me to display the proper error message ("Error Message I want to show"), as in any way to tie to the mvc attributes on the ViewModel. As another poster said this is a lot more scalable/re-usable and better design.

Using a data-for="Name" is very brittle as a change in the Model field name will not reflect there and that could be forgotten about hence delivering buggy software. You are losing the type safety of something like

@Html.ValidationMessageFor(m=> m.Name)

Code

public class AViewModel
{
       [Required(ErrorMessage="Error Message I want to show")]
        public string Name { get; set; }
}

<div class="validation-wrapper">
                <div class="input-wrapper">
                    @Html.TextBoxFor(m => m.Name, new { placeholder = "eg. John Smith", data_required_msg="PleaseEnter name", required="required" } )                           
                </div>
                <span class="k-invalid-msg" data-for="Name"></span>
            </div>

Cheers, J


回答1:


In order to be able to do what you are saying, you need to be using Kendo UI for ASP.NET MVC. With that you can continue using your DataAnnotations attributes and Html.ValidationMessageFor() helpers as before. All you will need to do is call $('[your_form_selector]').kendoValidator() after your form (or on document.ready()).



来源:https://stackoverflow.com/questions/16211169/kendo-ui-how-to-tie-validation-to-mvc-model-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!