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

[Solved] Model is null in DisplayTemplate for dropdowns in Grid

6 Answers 82 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jacques
Top achievements
Rank 1
Jacques asked on 19 Jul 2011, 04:51 AM
Hi,

I implemented a grid of towns (Villes) with inline editing. One of the fields contains dropdowns (server binding) of Regions from which is selected a region. In order to do so, the files ~/Views/DisplayTemplates/cbRegion.cshtml and ~/Views/EditingTemplates/cbRegion.cshtml were created and the model field was decorated with [UIHint("cbRegion"), Required].
There is no problem in edit mode since the dropdown is displayed nicelly.
The problem is in display mode where I get an error message from the DisplayTemplate file (cbRegion.cshtml) :

Object reference not set to an instance of an object.

Line 1:  @model Phare.Models.Region
Line 2: @Model.nom  <- error is here

The Grid definition is as below. Is there a setting or something else that I missed? Why is the model ok in edit mode (EditingTemplate file) and not in display mode? Is'nt the model passed the same way in both files?

Thanks

Grid definition:
@model IEnumerable<Phare.Models.Ville>
@{
    ViewBag.Title = "Villes";
}
<h2> Villes</h2>
 
@(Html.Telerik().Grid(Model)
    .Name("Grid")
    .ToolBar(commands => commands.Insert())
    .DataKeys(keys => keys.Add(c => c.ID))
    .Columns(columns =>
    {
        columns.Bound(o => o.ID);
        columns.Bound(o => o.nom);
        columns.Bound(o => o.Region); //Region is a complex type object for dropdown
        columns.Bound(o => o.description);
        columns.Command(commands => { commands.Edit(); commands.Delete(); }).Width(200);
    })
    .DataBinding(dataBinding => dataBinding
        .Server()
            .Select("Index", "Ville")
            .Insert("Insert", "Ville")
            .Update("Save", "Ville")
            .Delete("Delete", "Ville"))
    .Scrollable(scrolling => scrolling.Enabled(true))
    .Sortable(sorting => sorting.Enabled(true))
    .Pageable(paging => paging.Enabled(true))
    .Filterable(filtering => filtering.Enabled(true))
    .Groupable(grouping => grouping.Enabled(false))
    .Footer(true)
    .Editable(editing => editing.Mode(GridEditMode.InLine))
)

6 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 19 Jul 2011, 07:45 AM
Hello Jacques,

 We are not sure why the model is null in the display template. This can happen only if your grid is not server bound because we don't support display templates in ajax binding mode. Could you provide a sample project so we can check it out locally?

Regards,
Atanas Korchev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Jacques
Top achievements
Rank 1
answered on 19 Jul 2011, 02:50 PM
Hi Atanas,

Here is a sample project reproducing the problem.
0
Atanas Korchev
Telerik team
answered on 20 Jul 2011, 07:34 AM
Hello Jacques,

 I cannot run this sample project because it requires a database which is not present. You can either amend the project so it does not need a database or send a db backup.

Greetings,
Atanas Korchev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Jacques
Top achievements
Rank 1
answered on 20 Jul 2011, 02:00 PM

Sorry Atanas,

 

Here is the SQL Server 2008 Express backup file.

Thanks,

0
Atanas Korchev
Telerik team
answered on 21 Jul 2011, 08:33 AM
Hi Jacques,

 The problem is caused by the fact that there are some models whose Region property is actually null. This of course is not related to Telerik Grid for ASP.NET MVC.

I modified your project so it has a check if model is not null in the partial view.

Kind regards,
Atanas Korchev
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Jacques
Top achievements
Rank 1
answered on 21 Jul 2011, 01:09 PM

Thank you Atanas,

 

How did'nt I thought abput that...I will continue to use Telerik ASP.NET MVC grid since it's the best among those I tried latelly.

 

Thanks again,

 

Jacques

Tags
General Discussions
Asked by
Jacques
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Jacques
Top achievements
Rank 1
Share this question
or