Kendo ui - how to tie validation to mvc model attr

2019-07-21 06:53发布

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条回答
干净又极端
2楼-- · 2019-07-21 07:17

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()).

查看更多
登录 后发表回答