【问题标题】:Kendo MVC DropDownListFor does not bind selected value to modelKendo MVC DropDownListFor 未将选定值绑定到模型
【发布时间】:2016-05-04 04:32:32
【问题描述】:

@(Html.Kendo().DropDownListFor(model => model.ServiceID)
  .OptionLabelTemplate("#=optionLabel#")
  .ValueTemplate("#=Code#(#=Rate#) - #=Description#")
  .Template("#=Code#(#=Rate#) - #=Description#")
  .DataTextField("Code")
  .DataValueField("ServiceID")
  .DataSource(d =>
  {
    d.Read(read =>
    {
      read.Action("GetServiceRepository", "Service").Type(HttpVerbs.Post);
    });

  })
  .Events(e => e.Change("onWorkOrderJobServiceChange"))
  .HtmlAttributes(new { required = "required" })
  .OptionLabel(new { optionLabel = Resources.Wording.SelectOne, ServiceID = 0, Rate = 0, Code = "", Description = "" })
)

我有一个如上所述的下拉列表,并希望将所选值绑定到模型的 ServiceID 字段(类型为 int

但是,无论我选择哪个项目,ServiceID 字段始终为空!它甚至不是可空类型int?

为什么会发生这种情况,我该如何实现我想要达到的目标?

【问题讨论】:

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


    【解决方案1】:

    Kendo DropDownList 有 ValuePrimitive。默认为false,选中项返回Text和Value对。

    @(Html.Kendo().DropDownListFor(model => model.ServiceID)
      .ValuePrimitive(true)
      ....
    

    【讨论】:

    • 感谢您的解决方案,它有效!只有一件事让我感到困惑,那么 .DataValueField() 是什么意思?我认为它正在定义哪个字段用作选定值,但似乎选定值始终是 (Text, Value) 对?
    • 理想情况下,如果我们设置 DataValueField,我们应该得到 value 而不是 pair。不幸的是,事实并非如此。我同意它起初也让我感到困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    相关资源
    最近更新 更多