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

Child Grid Not Passing Correct Id to Controller

2 Answers 438 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shuja
Top achievements
Rank 1
Shuja asked on 19 May 2014, 04:44 PM
Hi

I'm using the latest Q1 2014 commercial updtaes for the UI toolkit.
I have a standard Parent-Child Kendo Grid where i can add and update a record in the parent and i wanted to do the same in the child grid. The parent grid worked fine but i couldn't get the child grid to insert a record. The code is as follows:

view:
<div style="width:800px;font-size:x-small; padding-top:40px;">
 
 
@(Html.Kendo().Grid<DRS2014.Models.PathAccountCode>()
        .Name("GridRules")
        .Columns(columns =>
        {
            columns.Bound(e => e.Id);
            columns.Bound(e => e.Debtors_Account_Code).Title("Debtors<br/>Account<br/>Code");
            columns.Bound(e => e.Email).Width(100);
            columns.Bound(e => e.EmailConfirmed).Title("Email<br/>Confirmed?").Width(40);
            columns.Bound(e => e.BackingReportFormat).Title("Report<br/>Format");
            columns.Bound(e => e.BackingReportType).Title("Report<br/>Type");
            columns.Bound(e => e.InvoiceType).Title("Invoice<br/>Type");
            columns.Command(command => { command.Edit().Text(" "); }).Width(80);
        })              
        .Sortable()
        .Pageable()
        //.Scrollable()
        .ClientDetailTemplateId("detailsTemplate")
        //.HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .Model(m=>m.Id(p=>p.Id))
            .PageSize(10)
            .Read(read => read.Action("GetPathAccounts", "Admin")) 
            .Create(create=>create.Action("InsertPathAccount","Admin"))
            .Update(update=>update.Action("UpdatePathAccount","Admin"))
            .Destroy(delete=>delete.Action("DeletePathAccount","Admin"))         
        )       
        .Events(e=>e.Edit("onEdit"))
        .ToolBar(commands=>commands.Create())
        .Editable(editable=>editable
        .Mode(GridEditMode.PopUp))
        .Filterable()
)
 
 
    <br />
 
 
</div>
 
<script id="detailsTemplate" type="text/kendo-tmpl">
   
 
    @(Html.Kendo().Grid<DRS2014.Models.PathAccountCust>()
        .Name("Data_#=Id#")
        //.Events(e=>e.Edit("onEdit2"))
        .Columns(columns=>
            {
                columns.Bound(o => o.CustGlobalCode);
                columns.Bound(o => o.CustName);
                columns.Bound(o => o.CustLocation);
                columns.Command(command => { command.Edit().Text(" "); });
            })
             
        .ToolBar(commands=>commands.Create())
        .Editable(editable=>editable
        .Mode(GridEditMode.PopUp))
             
        .DataSource(dataSource=>dataSource
        .Ajax()
    //    .Aggregates(agg=>
    //{
    //    agg.Add(p => p.Apportionment).Sum();
 
    //})
        .Model(m=>m.Id(x=>x.Id))
        //.Events(events => events.Error("error"))
        .PageSize(10)
         
         
        .Read(read=>read.Action("GetPathAccountCust","Admin", new { Id = "#= Id #" }))
        .Create(create=>create.Action("InsertPathAccountCust","Admin", new { Id = "#= Id #" }))
        .Update(update=>update.Action("UpdatePathAccountCust","Admin"))
        .Destroy(delete=>delete.Action("DeletePathAccountCust","Admin"))
         
      
        )
         
         //.Pageable(p=>p.Refresh(true))
        .Sortable()
        .ToClientTemplate())
 
 
    </script>


Controller:
public ActionResult InsertPathAccountCust([DataSourceRequest] DataSourceRequest request, Models.PathAccountCust newAccountCust, int Id)
{
    try
    {
        newAccountCust.AccountNoId = Id;
        _PriceRepository.insertPathAccountCodeCust(newAccountCust);
 
        return Json(new[] { newAccountCust }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {
        ModelState.AddModelError("ERR1", ex.Message);
 
        return Json(ModelState.ToDataSourceResult(), JsonRequestBehavior.AllowGet);
    }
 
}

When i break in the controller, the Id value is always 0. 
I checked in fiddler and it seems the view is passing back a value correctly as i can see the Id in there.

I tried many variations on this but could not get this to work at all.
However, I saw that both the parent and child grid had a primary key "Id" so i decided to test the system by changing the parent code to "pId".

View:
<div style="width:800px;font-size:x-small; padding-top:40px;">
 
 
@(Html.Kendo().Grid<DRS2014.Models.PathAccountCode>()
        .Name("GridRules")
        .Columns(columns =>
        {
            columns.Bound(e => e.pId);
            columns.Bound(e => e.Debtors_Account_Code).Title("Debtors<br/>Account<br/>Code");
            columns.Bound(e => e.Email).Width(100);
            columns.Bound(e => e.EmailConfirmed).Title("Email<br/>Confirmed?").Width(40);
            columns.Bound(e => e.BackingReportFormat).Title("Report<br/>Format");
            columns.Bound(e => e.BackingReportType).Title("Report<br/>Type");
            columns.Bound(e => e.InvoiceType).Title("Invoice<br/>Type");
            columns.Command(command => { command.Edit().Text(" "); }).Width(80);
        })              
        .Sortable()
        .Pageable()
        //.Scrollable()
        .ClientDetailTemplateId("detailsTemplate")
        //.HtmlAttributes(new { style = "height:430px;" })
        .DataSource(dataSource => dataSource
            .Ajax()
            .Model(m=>m.Id(p=>p.pId))
            .PageSize(10)
            .Read(read => read.Action("GetPathAccounts", "Admin")) 
            .Create(create=>create.Action("InsertPathAccount","Admin"))
            .Update(update=>update.Action("UpdatePathAccount","Admin"))
            .Destroy(delete=>delete.Action("DeletePathAccount","Admin"))         
        )       
        .Events(e=>e.Edit("onEdit"))
        .ToolBar(commands=>commands.Create())
        .Editable(editable=>editable
        .Mode(GridEditMode.PopUp))
        .Filterable()
)
 
 
    <br />
 
 
</div>
 
<script id="detailsTemplate" type="text/kendo-tmpl">
   
 
    @(Html.Kendo().Grid<DRS2014.Models.PathAccountCust>()
        .Name("Data_#=pId#")
        //.Events(e=>e.Edit("onEdit2"))
        .Columns(columns=>
            {
                columns.Bound(o => o.CustGlobalCode);
                columns.Bound(o => o.CustName);
                columns.Bound(o => o.CustLocation);
                columns.Command(command => { command.Edit().Text(" "); });
            })
             
        .ToolBar(commands=>commands.Create())
        .Editable(editable=>editable
        .Mode(GridEditMode.PopUp))
             
        .DataSource(dataSource=>dataSource
        .Ajax()
    //    .Aggregates(agg=>
    //{
    //    agg.Add(p => p.Apportionment).Sum();
 
    //})
        .Model(m=>m.Id(x=>x.Id))
        //.Events(events => events.Error("error"))
        .PageSize(10)
         
         
        .Read(read=>read.Action("GetPathAccountCust","Admin", new { pId = "#= pId #" }))
        .Create(create=>create.Action("InsertPathAccountCust","Admin", new { pId = "#= pId #" }))
        .Update(update=>update.Action("UpdatePathAccountCust","Admin"))
        .Destroy(delete=>delete.Action("DeletePathAccountCust","Admin"))
         
      
        )
         
         //.Pageable(p=>p.Refresh(true))
        .Sortable()
        .ToClientTemplate())
 
 
    </script>

Controller:
public ActionResult InsertPathAccountCust([DataSourceRequest] DataSourceRequest request, Models.PathAccountCust newAccountCust, int pId)
{
    try
    {
        newAccountCust.AccountNoId = pId;
        _PriceRepository.insertPathAccountCodeCust(newAccountCust);
 
        return Json(new[] { newAccountCust }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
    }
    catch (Exception ex)
    {
        ModelState.AddModelError("ERR1", ex.Message);
 
        return Json(ModelState.ToDataSourceResult(), JsonRequestBehavior.AllowGet);
    }
 
}

THIS WORKS! Just changing the parent primary Key to pId in the database and then changing all the references of this Id to "pId" allows the child grid to add and update records and the controller receives the correct Id passing to it.
But the question is why? Your examples have demos with both parent and child primary keys as "Id" and i have a colleague who has developed a similar parent-child grid and he uses "Id" in both tables too yet the kendo grid insert works for him.

My route config is:
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

I don't really want to be modifying my table primary key names so please could you let me know if i have missed something or whether there is a different problem with this grid setup that i've tried to implement?

Thank you,

Shuja

 



 

2 Answers, 1 is accepted

Sort by
0
Accepted
Petur Subev
Telerik team
answered on 21 May 2014, 12:07 PM
Hello Shuja,

The only issue that I can think off is that the name of the fields for both the parent and the child Grid is the same (let's say they are both 'ID'). Then on the server side you will receive same value for both cases, you should use different field names for the way of the transport (not need to change the model).

.Create(create=>create.Action("InsertPathAccountCust","Admin", new { parentId= "#= Id #" }))

Also you will have to change the name of the argument inside your action method. 

Kind Regards,
Petur Subev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Shuja
Top achievements
Rank 1
answered on 21 May 2014, 02:04 PM
Thank you!
That seems to work. Changed the name of the parent Id parameter being passed to the controller and named the parameter exactly the same in the Action and it works!
Tags
Grid
Asked by
Shuja
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Shuja
Top achievements
Rank 1
Share this question
or