Creating new row in nested Grid: How do I get parent key?

7 posts, 1 answers
  1. Shea
    Shea avatar
    58 posts
    Member since:
    Feb 2013

    Posted 19 Jul 2013 Link to this post

    I have spent the night searching, and can't seem to find an answer for what must be a fairly common question.

    I have a grid of 'Clients'. The client grids detail template is a grid of Licenses. Everything works fine, except when I add a row to the license grid, I can't  get access to the Client's Id column.

    ViewModel
    public class LicenseClient
    {
        public int Id { get; set;  }
        public String Name { get; set; }
        public String Email { get; set; }
        public String City { get; set; }
        public int NumLicenses { get; set; }
    }
     
    public class LicenseInfo
    {
        public int ClientId { get; set; }
        public Guid LicenseId { get; set; }
        public String Product { get; set; }
        public int Count { get; set; }
        public String Notes { get; set; }
    }
    My View:
    @{
        ViewBag.Title = "Index";
    }
     
    <h2>Client Licenses</h2>
     
    @(
        Html.Kendo().Grid<CoreLM.Models.ViewModels.LicenseClient>()
            .Name("clientGrid")
            .Columns( columns => {
                columns.Bound(c => c.Name);
                columns.Bound(c => c.City);
                columns.Bound(c => c.Email);
                columns.Bound(c => c.NumLicenses).Title("Total Licenses");
            })
            .DataSource(ds => ds.Ajax()
                .Read(r => r.Action("GetLicenseClients", "License"))
            )
            .Sortable()
            .Scrollable()
            .HtmlAttributes(new { style = "height:500px;" })
            .ClientDetailTemplateId("ClientLicenseDetailTemplate")
    )
     
    <script id="ClientLicenseDetailTemplate" type="text/kendo-tmpl" >
    @(
     Html.Kendo().Grid<CoreLM.Models.ViewModels.LicenseInfo>()
            .Name("clientLicensesGrid_#=Id#")
            .Columns(columns =>
            {
                columns.Bound(c => c.Product);
                columns.Bound(c => c.Count).Title("Licenses");
                columns.Bound(c => c.Notes);
                columns.Command(c => c.Destroy());
                columns.Command(c => c.Edit());
            })
            .DataSource(ds => ds.Ajax()
                .Read(r => r.Action("GetLicensesForClient", "License", new { clientId = "#=Id#" }))
                .Create(c => c.Action("CreateLicenseForClient", "License", new { clientId = "#=Id#}))
                .Update(u => u.Action("ChangeLicensesForClient", "License"))
                .Destroy(d => d.Action("DeleteLicensesForClient", "License"))
                .Model(m => {
                    m.Id(l => l.LicenseId);
                })
            )
            .ToolBar(tb => tb.Create())
            .Scrollable()
            .Sortable()
            .Editable()
            .ToClientTemplate()
    )
    </script>
    My Controller method for creating a new license:
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult CreateLicenseForClient([DataSourceRequest]DataSourceRequest request, int clientId, LicenseInfo li)
    {
                //clientId is always 0
    }

    I have even tried hard coding an Id, and it still doesn't work:
    .Create(c => c.Action("CreateLicenseForClient", "License", new { clientId = 99}))

    and in my controller method, 'clientId' is still 0.

    How can I pass the client (parent) id to the CreateLicenseForClient method? Without that id, I can't establish the Client has-many Licenses relationship.

    Thanks,
    ~S
  2. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2156 posts

    Posted 19 Jul 2013 Link to this post

    Hello Shea,


    This the correct way to pass the parent key to the Controller Actions and I was unable to reproduce the issue on my side and in the demos. The id is sent as expected. 

    The provided code seems correct, so probably the reason for the issue is somewhere else. Could you please send me a sample runnable project, where the issue is reproducing so I could inspect it locally and assist you further?

     

    Regards,
    Dimiter Madjarov
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Answer
    Shea
    Shea avatar
    58 posts
    Member since:
    Feb 2013

    Posted 19 Jul 2013 Link to this post

    I discovered the problem this morning, but haven't had a chance to post the solution back here.

    The fact that I was using 'clientId' as my route variable was the problem. It was trying to use the 'clientId' of the new row, which of course was 0. Changing the name of the route variable to 'parentId' fixed the problem.
    .Create(c => c.Action("CreateLicenseForClient", "License", new { parentId = "#=Id#}))
    and then in controller:
    public ActionResult CreateLicenseForClient([DataSourceRequest]DataSourceRequest request, LicenseInfo li, int parentId)
    {
          //....
    }
    Hope this helps someone else.
  5. David
    David avatar
    7 posts
    Member since:
    Jul 2013

    Posted 02 Aug 2013 Link to this post

    I'm having the same problem, can you post the rest of your fixed view?

  6. Rachael
    Rachael avatar
    27 posts
    Member since:
    Jul 2010

    Posted 14 Jun 2014 Link to this post

    Thank you for posting your fix! I had the exact same problem and changing the variable name indeed caused the value to be posted.
  7. Steve
    Steve avatar
    9 posts
    Member since:
    Oct 2014

    Posted 10 Mar in reply to Dimiter Madjarov Link to this post

    Dimiter,

    In the above example, how would you pass the value "clientId" to the UPDATE operation?   I've tried to do this an all I get are 0 values in the UPDATE() but I get a valid value in the CREATE()?

    Thanks,

    Steve

  8. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2156 posts

    Posted 14 Mar Link to this post

    Hello Steve,

    Passing the id value should be the same for all CRUD operations. If the problem is persisting, you could send us an isolated runnable example in which it is reproducing, here or in a support ticket, so we take a look at our end.

    Regards,
    Dimiter Madjarov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready