将kendo网格绑定到下拉列表,未填充时列显示“未定义”

时间:2019-10-02 20:04:51

标签: asp.net kendo-grid

我的剑道网格似乎有一个独特的问题。我到处搜索过,尝试过多种方法,但是没有运气。我需要将我的“ HoldReason”下拉列表绑定到kendo网格,并且“ HoldReason”将始终为null或最初未选择任何内容。目前,我的网格在该网格列中显示“未定义”。

ViewModel:

  public class PaymentDetailTerm
  {
    public PaymentDetailTerm()
    {
    }

    private int? holdReasonId;
    private bool holdFlag;
    private decimal disbursed;
    private SysHoldReason holdReason;

    //some properties omitted 

    [Display(Name = "Disbursed")]
    [NonNegative]
    [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Disbursed
    {
      get
      {
        return disbursed;
      }
      set
      {
        disbursed = value;
      }
    }

    [Display(Name = "Hold")]
    [BoolFieldRequiredWhenInt("HoldReasonId")]
    public bool HoldFlag
    {
      get
      {
        return holdFlag;
      }
      set
      {
        holdFlag = value;
      }
    }

    [Display(Name = "Hold Reason")]
    [IDFieldRequiredWhenBool("HoldFlag")]
    public int? HoldReasonId
    {
      get
      {
        return holdReasonId;
      }
      set
      {
        holdReasonId = value;
      }
    }

    [UIHint("HoldReasonEditor")]
    public SysHoldReason HoldReason
    {
      get
      {
        if (holdReason == null || holdReason.HoldReasonId == 0)
        {
          return new SysHoldReason() { HoldReasonId = 0, HoldReasonTitle = string.Empty };
        }
        else return holdReason;
      }
      set { holdReason = value; }   
    }
}

TemplateEditor“ HoldReasonEditor”:

  @using System.Collections
  @using Kendo.Mvc.UI


<kendo-dropdownlist name="HoldReasonId" bind-to="ViewBag.HoldReasonId" Auto-Width="true" option-label=" " datatextfield="Text" datavaluefield="Value" value-primitive="true"></kendo-dropdownlist>

控制器(这是有关下拉菜单的仅有的两个语句):

  ViewData["HoldReasonId"] = new SelectList(_context.SysHoldReason.Where<SysHoldReason>(x => x.SiteId == 99 || x.SiteId == _user.SiteId), "HoldReasonId", "HoldReasonTitle");
  ViewData["HoldReasonCollection"] = new SelectList(_context.SysHoldReason.Where<SysHoldReason>(x => x.SiteId == 99 || x.SiteId == _user.SiteId), "HoldReasonId", "HoldReasonTitle");

我将HoldReasonCollection用作隐藏字段,因此以后可以在网格保存中正确设置信息。 SysHoldReason模型包含HoldReasonId和HoldReasonTitle属性(还有更多,但这些是我关心的唯一属性)。

在我的视图中定义了剑道网格:

@(Html.Kendo().Grid<eCLERC.ViewModels.PaymentDetailTerm>(Model.PaymentDetailTerms)
  .Name("TermGrid")
  .DataSource(ds => ds
  .Ajax()
  .ServerOperation(false)
  .Model(m =>
  {
    m.Field(d => d.TermTypeTitle).Editable(false);
    m.Field(d => d.PayeePartyName).Editable(false);
    m.Field(d => d.PayoffBalance).Editable(false);
    m.Field(d => d.PeriodicAmount).Editable(false);
    m.Field(d => d.UnpaidBalance).Editable(false);
    m.Field(d => d.ClerkFeeFlag).Editable(false);
    m.Field(d => d.HoldFlag).Editable(true);
    m.Field(d => d.HoldReasonId).Editable(true);
    m.Field(d => d.HoldReason);
    //m.Field(p => p.HoldReason).DefaultValue(ViewData["HoldReasonId"] as eCLERC.Models.DB.SysHoldReason);
  })
  )
  .Columns(columns =>
  {
    columns.Bound(e => e.TermTypeTitle).Width(110).Title("Term Type");
    columns.Bound(e => e.PayeePartyName).Width(110).Title("Payee");
    columns.Bound(e => e.PayoffBalance).Width(110);
    columns.Bound(e => e.PeriodicAmount).Width(110); //TODO: Nikki - what is recap amount
    columns.Bound(e => e.UnpaidBalance).Width(100);
    columns.Bound(e => e.ClerkFeeFlag).Width(100);

    columns.Bound(e => e.Disbursed).Width(100);
    columns.Bound(e => e.HoldFlag).Width(100);
    columns.Bound(e => e.HoldReason).EditorTemplateName("HoldReasonEditor").ClientTemplate("#=HoldReason.Text#");
  })
  .Sortable()
  .Editable(editable => editable.Mode(GridEditMode.InCell))
  .Events(x => { x.Save("function(e){onGridSave(e)}"); })
  .Scrollable()
  .HtmlAttributes(new { style = "height:350px" }))

该下拉列表有效,如果我选择一个值,那么我会看到HoldReason标题值,但我只是想不出如何解决最初显示的“未定义”文本。同样,如果我要将下拉列表设置回选项标签(“”),则下拉控件将​​显示HoldReasonId所需的验证。

1 个答案:

答案 0 :(得分:0)

最后!所以,我明白了。首先,HoldReasonId必需的错误。我将控件绑定到整个代码表,ID,标题,描述等的数据库模型。这也是控制该代码表的数据条目的模型,因此需要ID值,这就是为什么得到那个错误。它给了我来自SysHoldReason数据库模型的验证。我通过为ViewSysHoldReason创建一个单独的视图模型来解决此问题,该模型仅包含可为空的HoldReasonId值和字符串HoldReasonTitle值。

  public class ViewSysHoldReason
  {
    public int? HoldReasonId { get; set; }
    public string HoldReasonTitle { get; set; }
  }

接下来,我更新了PaymentDetailTerm视图模型以将其用于编辑器,而不是像以前那样使用整个代码表数据库模型。

public class PaymentDetailTerm
  {
    public PaymentDetailTerm()
    {
    }

    private int? holdReasonId;
    private bool holdFlag;
    private decimal disbursed;
    private ViewSysHoldReason viewSysHoldReason;

    //some properties omitted 

    [Display(Name = "Disbursed")]
    [NonNegative]
    [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Disbursed
    {
      get
      {
        return disbursed;
      }
      set
      {
        disbursed = value;
      }
    }

    [Display(Name = "Hold")]
    [BoolFieldRequiredWhenInt("HoldReasonId")]
    public bool HoldFlag
    {
      get
      {
        return holdFlag;
      }
      set
      {
        holdFlag = value;
      }
    }

    [Display(Name = "Hold Reason")]
    [IDFieldRequiredWhenBool("HoldFlag")]
    public int? HoldReasonId
    {
      get
      {
        return holdReasonId;
      }
      set
      {
        holdReasonId = value;
      }
    }

    [UIHint("HoldReasonEditor")]
    public ViewSysHoldReason HoldReason
    {
      get
      {
        if (viewSysHoldReason == null || viewSysHoldReason.HoldReasonId == 0)
        {
          return new ViewSysHoldReason() { HoldReasonId = 0, HoldReasonTitle = string.Empty };
        }
        else return viewSysHoldReason;
      }
      set { viewSysHoldReason = value; }
    }
}

HoldReason上面的最后一个属性是此处所做的全部更改。接下来,我更新了网格以进行显示,如下所示:

  @(Html.Kendo().Grid<eCLERC.ViewModels.PaymentDetailTerm>(Model.PaymentDetailTerms)
      .Name("TermGrid")
      .DataSource(ds => ds
      .Ajax()
      .ServerOperation(false)
      .Model(m =>
      {
        m.Field(d => d.TermTypeTitle).Editable(false);
        m.Field(d => d.PayeePartyName).Editable(false);
        m.Field(d => d.PayoffBalance).Editable(false);
        m.Field(d => d.PeriodicAmount).Editable(false);
        m.Field(d => d.UnpaidBalance).Editable(false);
        m.Field(d => d.ClerkFeeFlag).Editable(false);
        m.Field(d => d.HoldFlag).Editable(true);
        m.Field(d => d.HoldReasonId).Editable(true);
        m.Field(d => d.HoldReason);
      })
      )
      .Columns(columns =>
      {
        columns.Bound(e => e.TermTypeTitle).Width(110).Title("Term Type");
        columns.Bound(e => e.PayeePartyName).Width(110).Title("Payee");
        columns.Bound(e => e.PayoffBalance).Width(110);
        columns.Bound(e => e.PeriodicAmount).Width(110); //TODO: Nikki - what is recap amount
        columns.Bound(e => e.UnpaidBalance).Width(100);
        columns.Bound(e => e.ClerkFeeFlag).Width(100);

        columns.Bound(e => e.Disbursed).Width(100);
        columns.Bound(e => e.HoldFlag).Width(100);
        columns.ForeignKey(p => p.HoldReason, (System.Collections.IEnumerable)ViewData["HoldReasonId"], "Value", "Text")
          .EditorViewData(new {HoldReasonId = (System.Collections.IEnumerable)ViewData["HoldReasonId"]})
          .EditorTemplateName("HoldReasonEditor").ClientTemplate("#=HoldReason.Value > 0 ? HoldReason.Text : '' #")
          .Title("Hold Reason")
          .Width(110);
      })
      .Sortable()
      .Editable(editable => editable.Mode(GridEditMode.InCell))
      .Events(x => { x.Save("function(e){onGridSave(e)}"); x.CellClose("function(e){onCellClose(e)}"); })
      .Scrollable()
      .HtmlAttributes(new { style = "height:350px" }))

请注意,我为CellClose添加了一个事件。这是因为当用户选择optionlabel =“”从网格中删除HoldReason时,我想抓住编辑器。一旦用户从下拉列表中选择一个值,我就无法弄清楚如何将该值设置为空,因此CellClose是我的解决方案。

function onCellClose(e) {
    var grid = $("#TermGrid").data("kendoGrid");
    var fieldName = grid.columns[e.container.index()].field;

    if (fieldName == "HoldReason" && e.container.find("input").val() == "") {
      e.model.HoldReasonId = 0;
      e.model.HoldReason.Value = 0;
      e.model.HoldReason.Text = "";
    }
  }

尽管没有onGridSave事件,但是无法正确设置模型,所以我觉得我也应该显示该代码。我没有在原始帖子中分享它。

function onGridSave(e) {
    e.preventDefault();
    var grid = $("#TermGrid").data("kendoGrid");
    var fieldName = grid.columns[e.container.index()].field;

    if (fieldName == "HoldReason") {
      $('#HoldReasonCollection').data('kendoDropDownList').value(e.container.find("input").val());
      e.model.HoldReasonId = e.values["HoldReason.HoldReasonId"];
      e.model.HoldReason = $('#HoldReasonCollection').data('kendoDropDownList').dataItem();
    }
    if (fieldName == "Disbursed") {
      e.model.Disbursed = e.container.find("input").val();
    }
    if (fieldName == "HoldFlag" && e.model.HoldFlag == true) {
      e.model.HoldFlag = false;
    }
    else if (fieldName == "HoldFlag" && e.model.HoldFlag == false) {
      e.model.HoldFlag = true;
    }
  }