我可以在 Kendo().DropDownList() 定义中添加条件吗?

Can I add conditions within Kendo().DropDownList() definition?

我有一个 kendo 下拉列表,我想向其中添加一个 属性,但前提是满足某些条件。如果可能的话,语法是什么?以下是我的想法。

@(Html.Kendo().DropDownList()
    .Name("My Dropdown List")
    .Value(Model.xxx)
     If (some condition){
       .Height(1000)
     }
    .DataTextField("MYDESCRIPTIEN")
    .DataValueField("MYFIELD")
    .HtmlAttributes(new { style = "width:300px" })
)

您好,您应该可以像这样添加 事件

@(Html.Kendo().DropDownList()
    .Name("My Dropdown List")
    .Value(Model.xxx)
    .DataTextField("MYDESCRIPTIEN")
    .DataValueField("MYFIELD")
    .HtmlAttributes(new { style = "width:300px" })
    .Events(e => e.Change("OnDropDownChanged"));
)

JAVASCRIPT

function OnDropDownChanged(e)
{
     //Do stuff to meet condition
}

更新:关于高度,恐怕你运气不好,因为 Height() 方法需要一个将始终序列化的非空整数值给客户。唯一的选择是在外部条件语句中使用两个不同的小部件声明。

===

每个流畅的方法都需要一个特定类型的值,或者一个 return 是这种类型的值的表达式。此外,每个配置设置都有一个默认值。

所以你有几个选择:

  • 根据条件使用 return 不同值的三元运算符。在一种情况下,它可能 return 属性 的默认值
  • 使用辅助变量,即预先赋值

可以以不同方式管理期望操作的 Fluent 方法,您可以使用标准条件语句,而不是三元语句。

以下是上述所有情况的示例:

@{     
   bool myCondition = false;
}

@(Html.Kendo().DropDownList()
    .HtmlAttributes(myCondition ? new { style = "width: 100%" } : new object { /* empty object */ } )
     .Events(e => {

          if (myCondition)
          {
              // nothing here this time
          }
          else
          {
              e.DataBound("onDataBound");
          }

    })
)

<script>

    function onDataBound(e) {
        console.log("dataBound");
    }

</script>