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

Add new with popup template driving me crazy

2 Answers 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Randy Hompesch
Top achievements
Rank 1
Randy Hompesch asked on 11 Sep 2019, 09:05 AM

I have a simple grid with a custom popup template for the editor. The Edit button pops the template and works great. However, the Create command on the toolbar is not responding at all. I get no errors and my custom popup template does not show.

Any help would be very much appreciated.

Thanks … Ed

 

 

    @(Html.Kendo().Grid<IndexModel.CertificateModel>()
.Name("grid")
.ToolBar(t =>
{
    t.Create().Text("Add New");
})
.HtmlAttributes(new { style = "height: 850px;" })
.Editable(e => e.Mode(GridEditMode.PopUp).TemplateName("CertificateEditTemplate")
    .Window(w => w.Title("Certificate").Width(650)))
.Events(evt => { evt.Edit("OnEdit"); })
            .Columns(columns =>
            {
                columns.Bound(c => c.CertId).Width(100).Visible(false);
                columns.Bound(c => c.Crop.CropName).Width(150);
                columns.Command(c => c.Edit().Text("Edit"));
             })
            .DataSource(ds =>
                                ds.Ajax()
                                .Batch(true)
                                .ServerOperation(false)
                                .Read(r => r.Url("?handler=CertsRead").Data("forgeryToken"))
                                .Update(u => u.Url("?handler=CertsUpdate").Data("forgeryToken"))
                                .Create(c => c.Url("?handler=CertsCreate").Data("forgeryToken"))
                                .Model(m =>
                                {
                                    m.Id(c => c.CertId);
 
                                })
                .PageSize(20)
 
                )
    )

 

2 Answers, 1 is accepted

Sort by
0
Randy Hompesch
Top achievements
Rank 1
answered on 11 Sep 2019, 01:48 PM
 

Well, it seems that the line where I say:

columns.Bound(c => c.Crop.CropName).Width(150);

 

Is not happy upon clicking the new button. I'm guessing it's because c.Crop is null and it's trying to access the cropname there.

The question then becomes, what's the right way to handle this?

I changed the model to have a direct property to the cropname and that fixed it. Not sure if that's the right way to go about things.

Thoughts?

Thanks … Ed

 

 

0
Tsvetomir
Telerik team
answered on 13 Sep 2019, 01:14 PM
Hi Randy,

It is correct that the issue stems from the fact that the Crop object is undefined at the moment a new item is added. Actually, the template that is used to display the property throws the error. What you can do is to handle the template as follows:

columns.Bound(p => p.Category.CategoryName).ClientTemplate("#=customTemplate(data)#");
 
<script>
    function customTemplate(dataItem){
        if (dataItem.Category) {
            return dataItem.Category.CategoryName;
        } else {
            return '';
        }
    }
</script>

Give this suggestion a try and let me know in case the issue is still present.


Best regards,
Tsvetomir
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Randy Hompesch
Top achievements
Rank 1
Answers by
Randy Hompesch
Top achievements
Rank 1
Tsvetomir
Telerik team
Share this question
or