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 566 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
1
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")))
Lorena
Top achievements
Rank 1
commented on 27 Jan 2023, 04:02 PM

After so many days I found this post!  It worked for me also! Thank you!, but I can't believe this is the answer.
Anton Mironov
Telerik team
commented on 31 Jan 2023, 09:14 AM

Hi Lorena,

If further assistance is needed, feel free to open a support ticket thread. 

Once we know what is needed, I and the team will try our best to assist with achieving the desired result.

Kind Regards,
Anton Mironov

Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
David
Top achievements
Rank 1
Share this question
or