ModelBinderException

Topics: General
Feb 12, 2014 at 3:07 AM
Edited Feb 12, 2014 at 3:09 AM
I'm having an issue with a ModelBinderException being thrown in the view when Weather records are added to a Terminal.

I can add the content type and all works fine, however I also have a view in the admin area which displays the Terminal content type and also items associated to it such as the weather information shown in the following example, and its from there I have the various options to add the 'child' items.

Initially of course there is nothing and the display page works fine and renders as expected, and if I click to add a weather item, the Create view is rendered, a description can be added, and the record is saved into the database as expected before then returning to the terminal detail view.

When accessing the Terminal detail view with a weather item attached, I can see that the weather collection has populated in the debugger, and as I enumerate the collection using a foreach, the object is populated (so not null), but the moment I try to use Razor to render a property, I get the error "Cannot Perform runtime binding on a null reference"



a) So first, an example of my TerminalPart showing the collection of weather items
public class TerminalPart : ContentPart<TerminalPartRecord>
{
     // lots of properties here etc

        public IEnumerable<TerminalWeatherInfoRecord> WeatherInformation
        {
            get { return Record.WeatherInformation; }
        }
}
b) And of course the corresponding record
public class TerminalPartRecord : ContentPartRecord
{
        public TerminalPartRecord()
        {
            WeatherInformation = new List<TerminalWeatherInfoRecord>();
        }

        [Required]
        public virtual string Designation { get; set; }
        public virtual float Latitude { get; set; }
        public virtual float Longitude { get; set; }
        .... etc etc


        [CascadeAllDeleteOrphan]
        public virtual IList<TerminalWeatherInfoRecord> WeatherInformation { get; set; }
    }
c) Now the definition of the TerminalWeatherInfoRecord itself....
    public class TerminalWeatherInfoRecord
    {
        public virtual int Id { get; set; }
        public virtual int TerminalPartRecord_Id { get; set; }
        public virtual string Description { get; set; }
    }
d) FInally, the view code which is failing......
        @foreach(var item in Model.WeatherInformation) {
        <tr>
            <td>@item.Description</td>
            <td class="action">
To clarify, If I debug, the code, Model.WeatherInformation is correctly populated so nHibernate has loaded the data, and as I enumerate the collection, the var 'item' is populated as well.... the error "Cannot Perform runtime binding on a null reference" occurs when the call to render @item.Description is run, .

I'm probably missing something ridiculously simple here, but its been a long day....
Feb 12, 2014 at 8:04 AM
After checking nHiberbate logs, i see the following...

2014-02-12 16:02:25,268 [35] NHibernate.Loader.Loader - total objects hydrated: 1
2014-02-12 16:02:25,269 [35] NHibernate.Engine.TwoPhaseLoad - resolving associations for [WCT.eFlight.Models.TerminalWeatherInfoRecord#4]
2014-02-12 16:02:29,491 [35] NHibernate.Engine.TwoPhaseLoad - adding entity to second-level cache: [WCT.eFlight.Models.TerminalWeatherInfoRecord#4]
2014-02-12 16:02:29,524 [35] NHibernate.Engine.TwoPhaseLoad - done materializing entity [WCT.eFlight.Models.TerminalWeatherInfoRecord#4]
2014-02-12 16:02:29,548 [35] NHibernate.Engine.Loading.LoadContexts - attempting to locate loading collection entry [CollectionKey[WCT.eFlight.Models.TerminalPartRecord.WeatherInformation#15]] in any result-set context
2014-02-12 16:02:29,549 [35] NHibernate.Engine.Loading.LoadContexts - collection [CollectionKey[WCT.eFlight.Models.TerminalPartRecord.WeatherInformation#15]] not located in load context
2014-02-12 16:02:29,549 [35] NHibernate.Engine.Loading.CollectionLoadContext - removing collection load entry [NHibernate.Engine.Loading.LoadingCollectionEntry<rs=NHibernate.Driver.NHybridDataReader, coll=[WCT.eFlight.Models.TerminalPartRecord.WeatherInformation#15]>@99861f] 2014-02-12 16:02:29,550 [35] NHibernate.Engine.Loading.CollectionLoadContext - 1 collections were found in result set for role: WCT.eFlight.Models.TerminalPartRecord.WeatherInformation
2014-02-12 16:02:29,551 [35] NHibernate.Engine.Loading.CollectionLoadContext - ending loading collection [NHibernate.Engine.Loading.LoadingCollectionEntry<rs=NHibernate.Driver.NHybridDataReader, coll=[WCT.eFlight.Models.TerminalPartRecord.WeatherInformation#15]>@99861f] 2014-02-12 16:02:29,551 [35] NHibernate.Engine.Loading.CollectionLoadContext - Caching collection: [WCT.eFlight.Models.TerminalPartRecord.WeatherInformation#15]
2014-02-12 16:02:29,552 [35] NHibernate.Engine.Loading.CollectionLoadContext - collection fully initialized: [WCT.eFlight.Models.TerminalPartRecord.WeatherInformation#15]
2014-02-12 16:02:29,552 [35] NHibernate.Engine.Loading.CollectionLoadContext - 1 collections initialized for role: WCT.eFlight.Models.TerminalPartRecord.WeatherInformation
2014-02-12 16:02:29,602 [35] NHibernate.Engine.StatefulPersistenceContext - initializing non-lazy collections
2014-02-12 16:02:29,627 [35] NHibernate.Loader.Loader - done loading collection
2014-02-12 16:02:29,662 [35] NHibernate.Event.Default.DefaultInitializeCollectionEventListener - collection initialized
2014-02-12 16:02:53,154 [35] Orchard.Data.SessionLocator - Transaction cancelled flag set
2014-02-12 16:02:53,155 [35] Orchard.Data.SessionLocator - Reverting operations from transaction
2014-02-12 16:02:53,156 [35] NHibernate.Transaction.AdoTransaction - Rollback
2014-02-12 16:02:53,157 [35] NHibernate.Transaction.AdoTransaction - IDbTransaction RolledBack
2014-02-12 16:02:53,158 [35] NHibernate.Transaction.AdoTransaction - IDbTransaction disposed.
2014-02-12 16:02:53,158 [35] NHibernate.Impl.SessionImpl - transaction completion
2014-02-12 16:02:53,159 [35] NHibernate.AdoNet.ConnectionManager - aggressively releasing database connection
2014-02-12 16:02:53,160 [35] NHibernate.Connection.ConnectionProvider - Closing connection
2014-02-12 16:02:53,163 [35] Orchard.Data.SessionLocator - Transaction disposed
2014-02-12 16:02:53,170 [35] Orchard.Environment.DefaultOrchardHost - EndRequest

Any ideas...