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

Error displaying child null object in mvc grid using razor

3 Answers 86 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.
Tim
Top achievements
Rank 1
Tim asked on 17 Nov 2011, 04:47 PM
Hi Guys,

I am having an issue displaying values when the child object is null in the client template. I am using the telerik MVC grid of the 2011.3.1115 release in MVC3 with razor.

I am trying to display a list of projects in the telerik mvc grid and the employees first name but some projects do not have an employee assigned to them so the value is null. This throws a javascript error when I try to access the value in the client template
 
"Microsoft JScript runtime error: Unable to get value of the property 'FirstName': object is null or undefined".

Is their a way to work around this or check if the employee is null and display string.empty using the razor syntax? I've attached some sample code below. Any help would be greatly appreciated!

Thanks

Ex: I have an object called Project with an employee property of type employee set up as below.

public class Project
    {
        public Employee Employee { getset; }
        public string ProjectName { getset; }		 
    }

    public class Employee
    {
        public string FirstName { getset; }
    }

here is the grid I am using.

@( Html.Telerik().Grid<ViewModels.ProjectView>()
        .Name("Grid")
        .ColumnContextMenu()                
        .Columns(columns =>
        {
            columns.Bound(o => o.Project.Project.ProjectName);                        
            columns.Bound(o => o.Project.Employee.FirstName).ClientTemplate("<#= Employee.FirstName#>").Title("PA");            
        })        
        .DataBinding(dataBinding =>
        {
            dataBinding.Ajax().OperationMode(GridOperationMode.Client)
                .Select("_MyProjectsGrid""Projects");
        })
        .Sortable()
        .Filterable()
        .Pageable(p => p.PageSize(20))
        .Resizable(r => r.Columns(true))        
        .NoRecordsTemplate("You have no deliverables at this time.")

3 Answers, 1 is accepted

Sort by
0
Michael
Top achievements
Rank 1
answered on 25 Nov 2011, 05:31 PM
I'm have the same problem !

Any solution?
0
Accepted
Michael
Top achievements
Rank 1
answered on 25 Nov 2011, 05:57 PM
columns.Bound(c => c.Produto).ClientTemplate("<#= Produto == null ? '' : Produto.Descricao #>")

I found this solution but the Filterable and Sortable donĀ“t work.

?
0
Tim
Top achievements
Rank 1
answered on 25 Nov 2011, 08:30 PM
I found this answer on stack overflow from what looks like one of the telerik guys. This is the same as your answer but your right; neither filter or sort work. This is definitely a bug and hopefully they will log and fix it. They are usually pretty good at doing that.

http://stackoverflow.com/questions/5260155/telerik-mvc-grid-and-sub-property-like-contact-name-when-the-contact-is-null 

You can do this by specifying the Template of the bound column:

columns.Bound(p => p.Contact.FullName)
       .Template(p => 
       {
          %>
             <%= (p.Contact != null ? p.Contact.FullName : "") %>
          <%
       }
       .Title("Contact")
       .Width(250);

Or you can use ClientTemplate:

columns.Bound(p => p.Contact.FullName)
       .ClientTemplate("<#= Contact !=null ? Contact.FullName : '' #>");
Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Michael
Top achievements
Rank 1
Tim
Top achievements
Rank 1
Share this question
or