Knockout + mvc 3 + Validation



  • 3 Answer(s)

       In  Mvc Controls Toolkit, A Helpers is  developed depend on the knockout library. These helpers help in knockout code and enhance the knockout library with Unobtrusive validation and globalization.In addition, the binding mechanism is enhanced to include complex controls such as a DatetimePicker, and other “complex” .

       At last,the knockout templates is find with Razor helpers.

       Look at the document hereherehere and here.some tutorials is there that show how to implement advanced features with helpers:

    Low BandWidth Transfers with The Client Side Templates of the Mvc Controls Toolkit

    Handling Big Amounts of Data with Client-Side Templates

    Handling Big Amounts of Data with Client-Side Templates 2

    Answered on November 17, 2018.
    Add Comment
    • For this, 1st convert the C# code into Javascript with their data type conversions etc.
    • write  two separate sets of validation at server and client side both.
    • It is very easier to control that than to write the own conversion framework.
    Answered on November 17, 2018.
    Add Comment

        Use the attributes validation by the own way:

    public class ModelWithValidation
    {
      public int Id { get; set; }
      [Required]
      public string Name { get; set; }
    }
    

       In mvc forms:

    @using( Html.BeginForm())
    {
      @Html.TextBoxFor(m => m.Name, new {data_bind = "value: name"})
      @Html.ValidationMessageFor(m => m.Name)
    }
    

       In jQuery test if form is good onSubmit or in knockout save function call the next code to validate input.

        It is must to include jQuery.unobtrusive* and jQuery.validate* libraries. Validate the input on server side also.

    var form = $("form");
    form.removeData('validator');
    form.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse(form);
    $("form").valid() //true false
    
    Answered on November 17, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.