【问题标题】:Client side validation "required" with conditional logic to kendo DropDownList from the ASP.NET MVC UI Api从 ASP.NET MVC UI Api 到 kendo DropDownList 的条件逻辑“需要”客户端验证
【发布时间】:2017-10-09 09:39:50
【问题描述】:

我正在为 ASP.NET MVC 使用剑道 UI。我想知道是否有一种方法可以使用 Razor 向 DropDownList 的 HtmlAttributes 添加条件逻辑。从我的例子中可以看出

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

我根据我的模型是否有 Id 来设置值。我想知道我的问题是否有语法。可能是这样的

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

我知道这可以通过 javascript 在元素的数据绑定事件上完成,但我的问题是在我的 razor 页面中是否有这样做的方法。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 kendo-ui kendo-asp.net-mvc


    【解决方案1】:

    Razor 本质上只是在视图中运行的 C#。您在这里尝试做的是将 if 语句包装在对象的范围内,即它不会编译。

    您可以做的最好的事情是将对象值从HtmlAttributes 方法中移出,然后使用if 语句将其拆分:

    @{
    object myAttributes = null;
    
        if(Model.DocumentId) {
            myAttributes = new { style = "width:100%", required = "required" }
        }
        else {
            myAttributes = new { style = "width:100%" }
        }
    }
    

    然后有

    @(Html.Kendo().DatePickerFor(model => model.RequestedDate)
        .Name("RequestedDate")
        .Format("dd/MM/yyyy")
        .HtmlAttributes(myAttributes)
        .Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
     )
    

    【讨论】:

      【解决方案2】:

      我赞成@ChrisC 的回答,因为他帮助我以我需要的方式看待它。我的处理方式是这样的:

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

      【讨论】:

        【解决方案3】:

        我是这样做的:

         @{ 
            dynamic obj = new ExpandoObject();
            if (Model.IsRequired)
            {
                obj.required = "required";
            }
            obj.style = "width:220px";
        }
        @(Html.Kendo().TextBoxFor(m => m.Value)
                .Enable(!Model.IsDisabled)
                .Value(Model.DefaultValue)
                .HtmlAttributes(obj)
                .Name(Model.Name))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-03-15
          • 2017-10-12
          • 2010-09-14
          • 2014-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多