1

I have an Asp.Net MVC 5 project. I am trying to utilize the DisplayTemplates to make my view more dynamic.

I am trying to create a display-template for any DateTime property. But I am unable to set ViewData.ModelMetadata.DisplayFormatString property for some reason.

Here is how my view-model looks like

public class DisplayClientViewModel : IMapFrom
{
    public int Id { get; set; }
    public string Name { get; set; }

    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime? EnrolledAt { get; set; }
}

Here is my DateTime.cshtml view

@model DateTime?

@if (!Model.HasValue && ViewData.ModelMetadata.NullDisplayText != null)
{
    <span>@ViewData.ModelMetadata.NullDisplayText</span>
}
else if (Model.HasValue)
{
    <span>@Model.Value.ToString(ViewData.ModelMetadata.DisplayFormatString ?? "g")</span>
}

Since my EnrolledAt property is decorated with [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")] I am expecting the ViewData.ModelMetadata.DisplayFormatString to be set to {0:MM/dd/yyyy} But it is always set to null for some reason.

How can I either set the ViewData.ModelMetadata.DisplayFormatString property on the ModelMetadata from the view-model, or access the value on DisplayFormat.DataFormatString?

Note: I am aware that I could decorate the property with [DataType(DataType.Data)] and create a Date.cshtmltemplate that would just display the short date. But in this case, I am hoping to be able to handle any format, not just Date or Time.

Junior
  • 11,602
  • 27
  • 106
  • 212
  • Assuming your `DateTime.cshtml` is in the `/Views/Shared/DisplayTemplates` folder, and y are using `@Html.DisplayFor(m => m.EnrolledAt)` in the main view, then `ViewData.ModelMetadata.DisplayFormatString` will not be `null` (so there is some other code you have not shown causing the issue. –  Aug 08 '18 at 22:45
  • But in any case, using `@Model.Value.ToString(ViewData.ModelMetadata.DisplayFormatString ?? "g")` would display "{0:08/09/2018}" based on today's date. It would need to be `@string.Format(ViewData.ModelMetadata.DisplayFormatString ?? "{0:g}", Model.Value)` –  Aug 08 '18 at 22:49
  • @StephenMuecke what could it be. I am showing the code how I am using it. Also, when I use it in a form `EditorFor` and with `[Required, DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]` it still showing the time in the textbox. regarding the formatting, how will that use the "g" format is there is no format given? – Junior Aug 08 '18 at 22:54
  • Because of `DisplayFormatString` is `null`, it uses the `"{0:g}"` format string. But I'm guessing that this is related to you previous questions where your using `object`? –  Aug 08 '18 at 22:56
  • Yes it is related to the previous question, I am using `Html.DisplayFor(x => columnMetadata.Model, columnMetadata.DataTypeName)` to display the value and using `EditorFor(x => x.PropertyName)` to display a text-box. https://stackoverflow.com/questions/51718933/how-to-render-modelmetadata-object-using-html-displar-in-razor-engine-in-asp-net – Junior Aug 08 '18 at 23:00
  • As I suspected :) - `columnMetadata.Model` passes just the value of the property. It does not have any metatdata. (and as a side note, `EditorFor()` is for `EditorTemplates` - i.e. for **editing** data and will contain form controls - your just displaying data, so use a `DisplayTemplate` and `DisplayFor()` –  Aug 08 '18 at 23:03
  • You could of course pass the `DataFormatString` to the template using the overload that accepts `additionalViewData`, but as I noted in your previous question, what you should be doing is creating a custom `HtmlHelper` extension methodto do this. (you are in for a world of pain if you continue down this path) –  Aug 08 '18 at 23:08
  • But there are many other metadata that would be helpful to pass like `NullDisplayText ` or `HtmlEncode` do I need to pass these manually? can't I just pass everything along? I think I am having the same problem with the Editor also... as I am not passing 3rd argument also – Junior Aug 08 '18 at 23:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177685/discussion-between-stephen-muecke-and-mike-a). –  Aug 08 '18 at 23:18

0 Answers0