MVC Grid Hierarchy not passing parent ID when creating Child record

2 posts, 1 answers
  1. David
    David avatar
    7 posts
    Member since:
    Jul 2013

    Posted 01 Aug 2013 Link to this post

    I'm having a problem adding a child record in my hierarchical grid.  It won't pass over the HeaderId from the parent in the grid.

    Here's the controller action:

    @(Html.Kendo().Grid<BillHeader>()
     
        .Name("BillHeaders")
        .Columns(columns =>
        {
            columns.Bound(h => h.BillHeaderId);
            columns.Bound(h => h.Category);
            columns.Bound(h => h.Description);
            columns.Bound(h => h.Amount);
        })
        .Pageable()
        .Selectable(selectable => selectable
                .Mode(GridSelectionMode.Multiple)
                .Type(GridSelectionType.Row))
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(6)
            .Events(events => events.Error("error_handler"))
            .Read(read => read.Action("BillHeaders_Read", "Bill"))
        )
        .Events(events => events.DataBound("dataBound"))
        .ClientDetailTemplateId("BillDetails")
     
          )
     
    <script id="BillDetails" type="text/kendo-tmpl">
        @(Html.Kendo().Grid<BillDetail>()
              .Name("BillDetails_#=BillHeaderId#")
              .Columns(columns =>
              {
                  columns.Bound(d => d.BillHeaderId).Width(50);
                  columns.Bound(d => d.BillDetailId).Width(70);
                  columns.Bound(d => d.Category).Width(70);
                  columns.Bound(d => d.Description).Width(150);
                  columns.Bound(d => d.Amount).Width(80);
                  columns.Command(command =>
                  {
                      command.Edit();
                      command.Destroy();
                  }).Width(75);
              })
              .DataSource(dataSource => dataSource
                  .Ajax()
                  .PageSize(10)
                  .Model(model =>
                  {
                      model.Id(d => d.BillDetailId);
                      model.Field(d => d.BillDetailId).Editable(false);
                  })
                .Events(events => events.Error("error_handler"))
                .Read(read => read.Action("BillDetails_Read", "Bill", new { billHeaderId = "#=BillHeaderId#" }))
                .Update(update => update.Action("BillDetail_Update", "Bill"))
                .Create(create => create.Action("BillDetail_Create", "Bill", new { billHeaderId = "#=BillHeaderId#" }))
                .Destroy(destroy => destroy.Action("BillDetail_Destroy", "Bill")))
               
              .Pageable()
              .ToolBar(tools => tools.Create())
              .ToClientTemplate()
              )
    </script>

    And here's the view.

    [AcceptVerbs(HttpVerbs.Post)]
           public ActionResult BillDetail_Create(BillDetail billDetail, int billHeaderId)
           {
               if (billHeaderId == 0)
               {
                   ModelState.AddModelError("billHeaderID", "add bill header first");
               }
               if (billDetail != null && ModelState.IsValid)
               {
                   var target = new BillDetail
                   {
                       Category = billDetail.Category,
                       Description = billDetail.Description,
                       Amount = billDetail.Amount,
                       BillHeaderId = billHeaderId,
                       BillDetailId = SessionBillDetails.Max(d => d.BillDetailId) + 1
                   };
     
                   //Get next Id in sequence
     
                   billDetail.BillDetailId = target.BillDetailId;
     
                   SessionBillDetails.Add(target);
               }
     
               return Json(new[] { billDetail }.ToDataSourceResult(new DataSourceRequest(), ModelState));
           }

    Can anyone spot an issue, or am I trying to do something that isn't possible?
    Thanks.


    edited: to add attachment example.
  2. Answer
    David
    David avatar
    7 posts
    Member since:
    Jul 2013

    Posted 02 Aug 2013 Link to this post

    Managed to finally fix this. Unbelievable really....I named the parameter in my controller (and view) to be "id"
    So Controller:

    public ActionResult BillDetail_Create(BillDetail billDetail, int id)


    And View:

     .Read(read => read.Action("BillDetails_Read", "Bill", new { id = "#=BillHeaderId#" }))<br>        .Update(update => update.Action("BillDetail_Update", "Bill"))<br>        .Create(create => create.Action("BillDetail_Create", "Bill", new { id = "#=BillHeaderId#" }))<br>        .Destroy(destroy => destroy.Action("BillDetail_Destroy", "Bill")))
Back to Top