Monday, 15 October 2012

Validate Upload control in KendoUI

.cshtml
<script type="text/javascript">

$(document).ready(function () {
   var validator = $("#ValidateDemoContainer").kendoValidator().data("kendoValidator");
       $("button").click(function (e) {
       //Validate Upload Control
       if ($("#UploadedFile").parent().siblings("ul").length > 0) {
               $("#UploadedFile").removeAttr("required");
       }
       //Validate Other control, which inside "ValidateDemoContainer"  
       if (!validator.validate()) {
               e.preventDefault();
            }
       });
});

</script>




 <div id="ValidateDemoContainer">
@using (Html.BeginForm("Method", "Controller", FormMethod.Post, new { id = "YourId", enctype = "multipart/form-data"}))
{
        @(Html.Kendo().Upload()
            .Name("UploadedFile")
            .Multiple(false)
            .HtmlAttributes(new { required = true })
        )
        <br />
        @Html.ValidationMessageFor(m => m.UploadedFile)
        <br />
        <button class="k-button" type="submit">Submit</button>
}
</div>
Model
public class YourModel
{
        [Required(ErrorMessage = "Please provide file")]
        public System.Web.HttpPostedFileBase UploadedFile { get; set; }
}

2 comments: