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

The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Int32'

1 Answer 465 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.
Koen
Top achievements
Rank 1
Koen asked on 04 Apr 2011, 12:46 PM
Hi,

I'm having a problem with binding the mvc grid.
An exception is thrown when binding:

The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Int32'.

Stacktrace:

   at System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value)
   at System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary)
   at System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions)
   at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate)
   at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData)
   at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper)
   at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData)
   at System.Web.Mvc.Html.EditorExtensions.EditorFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression)
   at Telerik.Web.Mvc.UI.Html.GridEditorForCellBuilder`2.AppendEditorFor(HtmlHelper`1 htmlHelper, IHtmlNode container)
   at Telerik.Web.Mvc.UI.Html.GridEditorForCellBuilder`2.AppendEditor(IHtmlNode container, HtmlHelper`1 htmlHelper)
   at Telerik.Web.Mvc.UI.Html.GridEditorForCellBuilder`2.AppendCellContent(IHtmlNode td, Object dataItem)
   at Telerik.Web.Mvc.UI.Html.GridDataCellBuilderBase.CreateCell(Object dataItem)
   at Telerik.Web.Mvc.UI.Grid`1.<>c__DisplayClass3b.<InitializeEditors>b__38(GridColumnBase`1 column)
   at Telerik.Web.Mvc.Extensions.EnumerableExtensions.Each[T](IEnumerable`1 instance, Action`1 action)
   at Telerik.Web.Mvc.UI.Grid`1.InitializeEditors()
   at Telerik.Web.Mvc.UI.Grid`1.WriteHtml(HtmlTextWriter writer)
   at Telerik.Web.Mvc.UI.ViewComponentBase.ToHtmlString()
   at Telerik.Web.Mvc.UI.ViewComponentBuilderBase`2.ToHtmlString()
   at Telerik.Web.Mvc.{Dynamic}.GridBuilder`1\[\[Ogone.FrontEnd.Dto.NCFrontEnd.SubscriptionOption\, Ogone.FrontEnd.Dto\, Version=1.0.0.0\, Culture=neutral\, PublicKeyToken=null\]\].IHtmlString.ToHtmlString()
   at System.Web.HttpUtility.HtmlEncode(Object value)
   at System.Web.WebPages.WebPageBase.Write(Object value)
   at ASP._Page_Views_Subscription_SubscriptionOptions_cshtml.Execute() in c:\Workdir\dotNet\FrontEnd.root\FrontEnd\FrontEnd.Web.BackOffice\Views\Subscription\SubscriptionOptions.cshtml:line 14
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)

It seems that is problem is already been discussed on this forum in the following thread:

http://www.telerik.com/community/forums/aspnet-mvc/grid/nullable-types-causes-column-binding-problem-version-2010-3-1110.aspx

Now this thread is from november last year, so I hoped the problem would be fixed with the new version of 2011.01.
Unfortunately this is not the case.
What can be a fix for this problem?

This is the code of the grid. The problem is the last column ConfirmationMemoId that is a nullable Int32.

@(Html.Telerik().Grid<Ogone.FrontEnd.Dto.NCFrontEnd.SubscriptionOption>()
        .Name("grid")
        .HtmlAttributes(new { @class = "gridTable", width = "100%" })
        .DataKeys(keys =>
        {
            keys.Add(s => s.OptionId);
            keys.Add(s => s.SubscriptionId);
            keys.Add(s => s.Modes);
        })
        .Columns(columns =>
        {
            columns.Command(commands => commands.Edit()).Title("Edit").Width(100);
            columns.Bound(s => s.OptionId)
                .ClientTemplate(Html.Ogone().ActionLink().Text("Delete").TargetAction("DeleteSubscription").RouteValues(new { Id = "<#= Id #>" }).ToString())
                .Title("Delete")
                .Sortable(false)
                .Filterable(false);
            columns.Bound(s => s.SubscriptionId).Visible(false);
            columns.Bound(s => s.OptionId).Width(100);
            columns.Bound(s => s.Modes).Width(100);
            columns.Bound(s => s.MonthlyFee).Width(100);
            columns.Bound(s => s.SetupFee).Width(100);
            columns.Bound(s => s.Currency).Width(100);
            columns.Bound(s => s.ActivByMerchant).Width(100);
            columns.Bound(s => s.ByDefault).Width(100);
            columns.Bound(s => s.Selectable).Width(100);
            columns.Bound(s => s.TXCost).Width(100);
            columns.Bound(s => s.TXRate).Width(100);
            columns.Bound(s => s.MinCost).Width(100);
            columns.Bound(s => s.MaxCost).Width(100);
            columns.Bound(s => s.Cost3DS).Width(100);
            columns.Bound(s => s.Comments).Width(100);
            columns.Bound(s => s.ConfirmationMemoId).Width(100);
        })
        .DataBinding(dataBinding => dataBinding
             .Ajax()
                 .Select("SubscriptionOptionListBinding""Subscription", Html.OGAntiForgeryTokenRouteValues("Subscription""SubscriptionOptionListBinding"new { subscriptionid = Model.SubscriptionId.ToString() }))
             .Update("_UpdateSubscriptionOption""Subscription", Html.OGAntiForgeryTokenRouteValues("Subscription""_UpdateSubscriptionOption"))
        )
        .Filterable()
        .Sortable(sorting =>
        {
            sorting.SortMode(GridSortMode.MultipleColumn);
            //sorting.OrderBy(sortOrder => sortOrder.Add(s => s.Id));
        })
)


Thanks for the help.

Best regards,

Koen

1 Answer, 1 is accepted

Sort by
0
Ta
Top achievements
Rank 1
answered on 11 Jan 2012, 05:50 PM
Just in case anyone else faces this issue...

I faced a similar issue. I was using the Integer UIHint for a nullable int property on my model. So when I bind or insert using the Integer editor template it would crash. I had to go into the Integer editor template and change it to a accept a nullable int instead of an int.

-Ta
Tags
Grid
Asked by
Koen
Top achievements
Rank 1
Answers by
Ta
Top achievements
Rank 1
Share this question
or