This is a migrated thread and some comments may be shown as answers.

MVC Grid Hierarchy not passing parent ID when creating Child record

1 Answer 299 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 01 Aug 2013, 03:30 PM
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.

1 Answer, 1 is accepted

Sort by
0
Accepted
David
Top achievements
Rank 1
answered on 02 Aug 2013, 11:34 AM
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")))
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
David
Top achievements
Rank 1
Share this question
or