【问题标题】:kendodropdownlist not binding value to model after postkendodropdownlist 在发布后未将值绑定到模型
【发布时间】:2017-03-09 15:53:40
【问题描述】:

我正在为我的项目使用剑道 UI。我有一个用 json 填充的剑道下拉列表。我在我的下拉列表中获得了值,但是在发布时,模型没有获得下拉列表的选定值。我已经坚持了一天没有结果。我不确定我要去哪里。请检查代码

查看:

      @model IEnumerable<EntityFrameworkClasses.StaggingException>



                @foreach (var item in Model)
    {




        @(Html.Kendo().DropDownListFor(modelItem => item.Level2)
                  .Name("Level2")
                  .HtmlAttributes(new { style = "width:10%" })
                  .OptionLabel("Select level 2...")
                  .DataTextField("Text")
                  .DataValueField("Value")
                  .BindTo((System.Collections.IEnumerable)ViewBag.Level2)

        )
}


@(Html.Kendo().Grid(Model)
    .Name("CashExceptionsGridTest")
    .Columns(columns =>
    {



        columns.Bound(p => p.Category).Title("Category").Width(130);
        columns.Bound(p => p.EnterText1).Title("Comments").Width(130);
        columns.Bound(p => p.Dateoftransaction).Title("Date").Width(130);
        columns.Bound(p => p.InternalLocalAmount).Title("InternalAmt").Width(130);
        columns.Bound(p => p.ExternalLocalAmount).Title("ExternalAmt").Width(130);

    })
          .ToolBar(toolbar =>
                {
                     //toolbar.Template("<a class='k-button k-button-icontext' onclick='customCommand()' href='#'></span>Cutom Command</a>");

                         toolbar.Create(); // The "create" command adds new data items.
                          toolbar.Save();// The "save" command saves the changed data items.

                      })
        .Editable(editable => editable.Mode(GridEditMode.InCell)) // Use in-cell editing mode.
        .HtmlAttributes(new { style = "height: 550px;" })



    .HtmlAttributes(new { style = "height: 350px;" })
    .Pageable(pageable => pageable
    .Input(true)
    .Numeric(false)
    )
           .Reorderable(r => r.Columns(true))
             .Sortable()
             .ColumnMenu()
             .Scrollable(scr => scr.Height(430))
             .Filterable()
    .DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .ServerOperation(false)


                 .Batch(true) // Enable batch updates.
                           .Model(model =>
                                {
                                    model.Id(p => p.RowID); // Specify the property which is the unique identifier of the model.
                                    model.Field(p => p.RowID).Editable(false); // Make the ProductID property not editable.
                                })



              .Update("Editing_Update", "MultiTab")
              .Create("Editing_Create", "MultiTab")
               )




)
}

为了代码简洁,我有一个剑道网格,下面我不包括。

控制器:

  public ActionResult GetLevel()
            {
       IEnumerable<SelectListItem> Level2 = db2.StaggingInternalCashExceptions.Where(x=>x.LoadID==loadid).Select(c => new SelectListItem
            {

                Value = c.Level2.ToString(),
                Text = c.Level2

            }).Distinct();

            return  View();

            }

  [AcceptVerbs(HttpVerbs.Post)]
         public ActionResult Editing_Create([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<StaggingException> results)
         if (results != null && ModelState.IsValid)
         {
             foreach (var result in results)
              var entity = new StaggingException();
                     entity.RowID = result.RowID;
                     entity.Category = result.Category; //this is a textbox in the view for which i get the value
                     entity.Level1 = result.Level1; //gives null
//I'm adding those values to the db. Didn't include all that for the sake of keeping it short.
}
}

网格具有批量编辑功能,一旦我点击保存更改,网格的数据就会发布到控制器,我可以在结果中看到它。我无法获得下拉值。 请有任何想法或线索。 谢谢。

【问题讨论】:

  • 我假设这是一个局部视图 - 我注意到您的操作指定了绑定前缀 - 是否使用相同的前缀调用局部视图?
  • @sleeyuen:这不是部分观点。我想在同一个视图中显示一个下拉列表和一个剑道网格。我使用@Html.kendo.DropdownListFor(modelItem=>item.Level1) 将下拉列表绑定到模型,但在发布后我无法获得令人费解的下拉列表的选定值。
  • OK - 接下来我要检查的是表单是否真的向您的控制器发布任何内容(通过 Chrome 开发工具、Fiddler 等)。如果要发布数据,请将其包含在此处,以便我更好地了解可能出现故障的位置。
  • @sleeyuen:我添加了更多代码。把它放在提琴手中,由于整个模型,这会有点困难。我在操作方法上放了一个调试点,我可以在结果中看到网格数据,但我无法获取选定的下拉值。
  • 我想我明白你在这里得到了什么,但是为了确认,你能发布一张渲染页面的图像吗?我并不是要创建一个工作小提琴 - 我的意思是使用一个名为 Fiddler 的程序来查看哪些数据正在从浏览器传递到您的控制器,就像您可以使用 Chrome/IE 开发工具上的网络选项卡一样检查网络活动。

标签: asp.net-mvc-4 kendo-ui kendo-asp.net-mvc kendo-dropdown


【解决方案1】:

这篇文章迟到了,但我遇到了类似的问题,对于那些有相同问题的值未在模型中发布的人,如果您的模型值是可为空的 int? (不能从上面的帖子中看到......)那么您需要如下配置DropDownListFor以避免默认值绑定行为以在初始值为null时使用所选项目更新字段。希望这对某人有所帮助。

Html.Kendo().DropDownListFor(m => m)
.HtmlAttributes(new { data_value_primitive = "true" })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    相关资源
    最近更新 更多