Home » ASP » ASP.MVC » Integer DisplayTemplates, EditorTemplates and Nullable Value

Integer DisplayTemplates, EditorTemplates and Nullable Value

You can define templates in the DisplayTemplates and EditorTemplates. If you define one template for integer your can have an error saying that the model item require a string.

The model item passed into the dictionary is of type ‘System.Int32’, but this dictionary requires a model item of type ‘System.String’

This error occurs if you name the template int.cshtml or integer.cshtml. Even if you have defined the @model int or @model int?.

The solution is to name the file Int32.cshtml. CLR type name are used and not C# alias.

Another detail important to remember is that if you want to have integer and nullable integer you can only do it withing one template. So, always define the type with its nullable type. For example, the integer should be in the file Int32.cshtml and the template could be something like this (for DisplayTemplate):

@model int?
    @if (Model.HasValue)
    {
        @Html.Raw(Model)
    }
    else
    {
        @:-
    }

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

One Response so far.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.