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

[Solved] Issue with batch insert in detail grid

2 Answers 23 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tonny
Top achievements
Rank 1
Tonny asked on 20 Dec 2011, 12:07 PM
Hi all

I'm experiencing an issue when trying to add rows in a second-level grid using InCell editing.

After doing the batch-update, the grid keeps new rows in their edited-state. This makes the user doubt whether the update actually took place and means that any further edits result in inserting the row again.

This is my code.

The grid:
@(Html.Telerik().Grid<PriceAdjustmentsViewModel>()
        .Name("AgeAdjustGrid")
        .DataKeys(keys => keys.Add(o => o.Id))
        .DataBinding(bind => bind.Ajax()
            .Select("BindPriceAdjustments", "Adjustments", new { SeasonId = Model.Id })
            .Update("UpdatePriceAdjustment", "Adjustments")
            .Delete("DeletePriceAdjustment", "Adjustments")
        )
        .Columns(columns =>
        {
            columns.Bound(o => o.Name).Width(360);
            columns.Command(commands =>
            {
                commands.Edit().ButtonType(GridButtonType.Image);
                commands.Delete().ButtonType(GridButtonType.Image);
            });
        })
        .Footer(false)
        .ClientEvents(events => events.OnRowDataBound("ExpandRow"))
        .DetailView(detail => detail.ClientTemplate(
            Html.Telerik().Grid<AgeIntervalsViewModel>()
                .Name("AgeInterval<#= Id #>")
                .DataKeys(keys => keys.Add(o => o.Id))
                .Editable(editing => editing.Mode(GridEditMode.InCell))
                .DataBinding(bind => bind.Ajax()
                    .Select("BindAgeAdjustments", "Adjustments", new { PriceAdjustmentId = "<#= Id #>" })
                    .Update("UpdateAgeAdjustments", "Adjustments", new { PriceAdjustmentId = "<#= Id #>" })
                )
                .ToolBar(commands =>
                {
                    commands.Insert().ButtonType(GridButtonType.Image);
                    commands.SubmitChanges().ButtonType(GridButtonType.Image);
                })
                .Columns(subcolumns =>
                {
                    subcolumns.Bound(o => o.AgeFrom);
                    subcolumns.Bound(o => o.AgeTo);
                    subcolumns.Bound(o => o.Amount);
                    subcolumns.Bound(o => o.Auto).ClientTemplate("<#= Auto ? 'Auto' : 'Manuel' #>");
                    subcolumns.Command(command => command.Delete().ButtonType(GridButtonType.Image));
                })
                .Footer(false)
                .ToHtmlString()
        ))
)

The auto-expand script I'm using onRowDatabound:
<script type="text/javascript">
    function ExpandRow(e) {
        $("#AgeAdjustGrid").data("tGrid").expandRow(e.row);
    }
</script>


The controller update and bind functions:
[GridAction]
public ActionResult BindAgeAdjustments(int PriceAdjustmentId)
{
    ProTravelDB Db = MyDatabase.GetProTravelDB();
    PriceAdjustmentsViewModel PA = new PriceAdjustmentsViewModel((from PAs in Db.PriceAdjustments where PAs.Id == PriceAdjustmentId select PAs).First());
 
    return View(new GridModel(PA.AgeIntervals));
}
 
[GridAction]
public ActionResult UpdateAgeAdjustments(int PriceAdjustmentId,
                                         [Bind(Prefix = "inserted")]IEnumerable<AgeIntervalsViewModel> inserted,
                                         [Bind(Prefix = "updated")]IEnumerable<AgeIntervalsViewModel> updated,
                                         [Bind(Prefix = "deleted")]IEnumerable<AgeIntervalsViewModel> deleted)
{
    ProTravelDB Db = MyDatabase.GetProTravelDB();
    if (inserted != null)
    {
        foreach (AgeIntervalsViewModel PA in inserted)
        {
            PriceAdjustmentAgeIntervals interval = new PriceAdjustmentAgeIntervals();
            interval.AgeFrom = PA.AgeFrom;
            interval.AgeTo = PA.AgeTo;
            interval.Amount = PA.Amount;
            interval.Auto = PA.Auto;
            interval.PriceAdjustmentId = PriceAdjustmentId;
            Db.Add(interval);
            Db.SaveChanges();
        }
    }
    if (updated != null)
    {
        foreach (AgeIntervalsViewModel PA in updated)
        {
            PriceAdjustmentAgeIntervals PaAIToUpdate = (from PAAIs in Db.PriceAdjustmentAgeIntervals where PAAIs.Id == PA.Id select PAAIs).First();
            PaAIToUpdate.AgeFrom = PA.AgeFrom;
            PaAIToUpdate.AgeTo = PA.AgeTo;
            PaAIToUpdate.Amount = PA.Amount;
            PaAIToUpdate.Auto = PA.Auto;
            Db.SaveChanges();
        }
    }
    if (deleted != null)
    {
        foreach (AgeIntervalsViewModel PA in deleted)
        {
            PriceAdjustmentAgeIntervals PaAIToDelete = (from PAAIs in Db.PriceAdjustmentAgeIntervals where PAAIs.Id == PA.Id select PAAIs).First();
            Db.Delete(PaAIToDelete);
            Db.SaveChanges();
        }           
    }
    return View(new GridModel(new PriceAdjustmentsViewModel((from PAs in Db.PriceAdjustments where PAs.Id == PriceAdjustmentId select PAs).First()).AgeIntervals));
}

Any hints on what I'm doing wrong here will be greatly appreciated.

TIA

2 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 22 Dec 2011, 07:51 AM
Hi Tonny,

Since i was unable to reproduce the behavior you described using the code you shared, I would like to ask you to send us an isolated working sample implementing your current logic and setup, .
Thank you for the assistance and cooperation.

Best wishes,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Tonny
Top achievements
Rank 1
answered on 22 Dec 2011, 10:30 AM
Hi Petur

Thanks for the reply.

After posting my question, I build further views based on the faulty one, which did not themselves fail, so in a sense I was unable to reproduce the error myself. This allowed me the opportunity to play "spot the difference" with my own code, though, and I eventually found that the flaw was in my view model: a helper property which wasn't included in the grid had an errant [Required] tag, causing validation to fail. Removing the un-needed tag fixed the issue.

I build my code for handling the update on your example in the documentation and, while the error is entirely mine, I suggest you add a note about how errors in model state are handled by the grid, because the behaviour really stumped me, here. A single line about the grid edit state not clearing if the model state contains an error would have given me the answer right away.

Best wishes to you, also.

/T
Tags
Grid
Asked by
Tonny
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Tonny
Top achievements
Rank 1
Share this question
or