Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8066475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:00:32+00:00 2026-06-05T12:00:32+00:00

I want to create a display template for string properties only, and use default

  • 0

I want to create a display template for string properties only, and use default for all others.

I tried to make a string.cshtml in Views/Shared/DisplayTemplates with following contents:

@model string
@Html.TextBoxFor(m => m, new { @readonly = "readonly" })

I now have a problem when I try to open any view which uses DisplayFor(m => m.property), it shows error like:
The model item passed into the dictionary is of type ‘System.DateTime’, but this dictionary requires a model item of type ‘System.String’.
or:
The model item passed into the dictionary is of type ‘System.Int64’, but this dictionary requires a model item of type ‘System.String’.

I know that I can solve this by adding display template for every type used, but I suppose that it is also possible to use “default” template for all types where custom template is not defined?

UPDATE
After Darin’s answer, I checked Brad’s tutorial and changed template into:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @readonly = "readonly" })

This is based on “default” template and works for all types.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:00:35+00:00Added an answer on June 5, 2026 at 12:00 pm

    There are 9 built-in display templates: “Boolean”, “Decimal”, “EmailAddress”, “HiddenInput”, “Html”, “Object”, “String”, “Text”, and “Url”.

    Take a look at the following blog post which explains in details how templates work and how are they resolved. Here’s a quote from it:

    The following template names are tried in order:

    1. TemplateHint from ModelMetadata
    2. DataTypeName from ModelMetadata
    3. The name of the type (see notes below)
    4. If the object is not complex: “String”
    5. If the object is complex and an interface: “Object”
    6. If the object is complex and not an interface: Recurse through the inheritance hiearchy for the type, trying every type name

    So you are hitting point 4. for DateTime and Int64 because there’s no default template for those types.

    So you could use Template hints or DataType names from the ModelMetadata to use this custom template only for given properties on your view model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a Repeater that displays the header/footer based on properties, only
I want to create application which would display spatial data on Map. I'm thinking
I want to create a UI that uses a tab control to display multiple
I want to display a file tree similarly to java2s.com 'Create a lazy file
I want create module which update list of usb devices automatically (not only mass
I have a list of values I want to display in a template of
My web app has a template that is used in all pages. I want
I have a couple of properties in my view model that are display-only but
I'm trying to create a basic template to display the selected instance's field values,
I want to create extensivy page template (not layout template) programmatically, something like Blog-

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.