Search code examples
c#asp.net-mvcasp.net-mvc-4kendo-uikendo-asp.net-mvc

Client side validation "required" with conditional logic to kendo DropDownList from the ASP.NET MVC UI Api


I am using the kendo UI for ASP.NET MVC. I was wondering if there was a way to add conditional logic to the HtmlAttributes of a DropDownList using Razor. As you can see from my example

@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
    .Name("RequestedDate")
    .Format("dd/MM/yyyy")
    .HtmlAttributes(new { style = "width:100%" })
    .Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
 )

I am setting the value according to if my model has Id or not. I was wondering if there was a syntax for my question. Possibly something like that

@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
    .Name("RequestedDate")
    .Format("dd/MM/yyyy")
    .HtmlAttributes(new { style = "width:100%" if(Model.DocumentId){ required = "required" })
    .Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
 )

I know it can be done through javascript on the databound event possibly of the element but my question is if there is a way of doing this in my razor page.


Solution

  • I upvoted @ChrisC answer because he helped me see it the way I needed. The way I handled it is like that:

    @(Html.Kendo().DatePickerFor(model => model.RequestedDate)
        .Name("RequestedDate").Format("dd/MM/yyyy")
        .HtmlAttributes(Model.DocumentId == null ? (object) new { style = "width:100%" } : new { style = "width:100%", required = "required" })
         .Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
    )