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

Updating dataSource after Adding a new item in Grid

0 Answers 122 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 1
Matt asked on 30 Oct 2012, 11:09 PM
I have the Grids set up in a few places on my site and they are working well except when I create a new item.  The item is sent to the controller correctly, written to the database and then wrapped in JSON with the new ID and sent back out to the site correctly (I tracked it in Firebug and the JSON is well formed with no errors).  How do I bind this back to the dataSource and replace the new line?  If I click Update on the row it creates another new item instead of updating the current.

Grid Code
<%: Html.Kendo().Grid<Thread.Data.Models.Model>()
        .Name("Grid")
        .Columns(columns =>
        {
           columns.Command(command => { command.Edit(); command.Destroy(); }).Width(190).HtmlAttributes(new { style = "text-align:center;" });
           columns.Bound(m => m.ModelID).Hidden();
           columns.Bound(m => m.ModelName).Width(140);
           columns.Bound(m => m.Company).Width(160).ClientTemplate("#= (typeof Company === 'undefined') ? ' ' : Company.CompanyName #").EditorTemplateName("CompanyDropDownList");
           columns.Bound(m => m.Job).Width(160).ClientTemplate("#= (typeof Job === 'undefined') ? ' ' : Job.JobName #").EditorTemplateName("JobDropDownList");
           columns.Bound(m => m.TempModelNumberFlag).Width(100);
           columns.Bound(m => m.IsCarryOver).Width(100);
           columns.Bound(m => m.HoursEstimate).Width(120).ClientTemplate("#= (HoursEstimate === null) ? ' ' : HoursEstimate #");
           columns.Bound(m => m.Description).Width(140).ClientTemplate("#= (Description === 'null') ? ' ' : Description #");
           columns.Bound(m => m.RetailIntroDate).Width(140).Format("{0:d}");
           })
        .ClientDetailTemplateId("jobDetailTemplate")
        .ToolBar(toolBar =>
        {
            toolBar.Create();
        })
        .Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.InLine))
        .HtmlAttributes(new { style = "height: 675px" })
        .Pageable()
        .Sortable()
        .Scrollable()
                .Selectable()
        .Filterable()
        .Events(events => events.DataBound("dataBound"))
        .DataSource(dataSource => dataSource
            .Ajax()
            .ServerOperation(false)
            .PageSize(15)
            .Events(events => events.Error("error_handler"))
            .Model(model =>
            {
                model.Id(j => j.ModelID);
                model.Field(j => j.ModelID).Editable(false);
                model.Field(j => j.AlertCount).Editable(false);
                model.Field(j => j.ArticleCount).Editable(false);
                model.Field(j => j.DocumentCount).Editable(false);
            })
            .Read(read => read.Action("Model_Read", "Models"))
            .Update(update => update.Action("Model_Save", "Models"))
            .Create(create => create.Action("Model_Save", "Models"))
            .Destroy(destroy => destroy.Action("Model_Destroy", "Models"))
        )
    %>

In the Controller, after the save I return this:
return Json(new[] { results.ToList()[0] }.ToDataSourceResult(request, ModelState));

The JSON returned is fine and has the ID:
Data
[Object { ModelID=6
,  ModelName="Test IV"
,  ModelNumber="DSAF",  more...}]
     
Total 1
AggregateResults null
Errors null

Can this rebind or do I have to call Read and reload the whole dataSource?  And if so how do I do either one of those.

Thanks,

Matt

No answers yet. Maybe you can help?

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