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 6542501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:14:08+00:00 2026-05-25T11:14:08+00:00

I am trying to access the DataTypeName from the ModelMetadata in a custom DisplayTemplate,

  • 0

I am trying to access the DataTypeName from the ModelMetadata in a custom DisplayTemplate, but it is always null. My goal is to make a decimal.cshtml template that looks for the DataTypeName and if it’s equal to Currency format it as currency. Otherwise display the decimal like normal.
Here’s an example of what I have now:

The Model:

public class MyModel
{
    [DataType(DataType.Currency)]
    public decimal foo { get; set; }
}

In a view that is strongly typed to MyModel I have a call like this: @Html.DisplayFor(m => m.foo)

The Display template (~/Views/Shared/DisplayTemplates/decimal.cshtml)

@model decimal?
@{
    var type = ViewData.ModelMetadata.DataTypeName; // This is always null
}

I know that it’s using my DisplayTemplate like it’s supposed to, but I would expect the DataTypeName to not be null in this case since I have the DataType attribute on the model. Is there any way to access the DataType in a display template like this?

  • 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-05-25T11:14:08+00:00Added an answer on May 25, 2026 at 11:14 am

    Strange, it should work. I am unable to reproduce. Here’s what I tried in a new application:

    Model:

    public class MyModel
    {
        [DataType(DataType.Currency)]
        public decimal foo { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyModel
            {
                foo = 12.5m
            });
        }
    }
    

    View (~/Views/Home/Index.cshtml):

    @model MyModel
    @Html.DisplayFor(x => x.foo)
    

    Display template (~/Views/Home/DisplayTemplates/decimal.cshtml):

    @model decimal?
    @ViewData.ModelMetadata.DataTypeName
    

    When I run the application it prints Currency as expected.

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

Sidebar

Related Questions

I'm trying to access YouTube from within an iOS app, but the iOS SDK
Trying to access a Servlet from a button on HTML page //Html Page FORM
I'm trying to access a member structs variables, but I can't seem to get
Im trying to access ValidationMessage through jquery. I have <%: Html.TextBox(vcr_LinkAddress,null, new { maxlength
I am trying to access Facebook from Python :D I want to fetch some
I'm trying to access 1.20163 from the following: 1 British Pound Sterling = 1.20163
I am trying to access the outer class variable cell[i][j] from the inner class
Im trying to access some variables from the parent in a child mc. Parent
I am trying to access the raw pixels of an image, but I keep
i cannot get current content page html. when i trying access from script in

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.