Is it possible to place a conditional statement inside an Html.DisplayFor call?

Basically, I have a custom built EndDate Date class in my MVC output model. FYI: The Date class builds a DateTime, but hides the functionality of time. I created a display template for this type of date that formats the date nicely, but in the instance instance (shown below), if the object is null (in this case for EndDate), I would like to display the text β€œNo end date” instead.

<%:Html.DisplayFor(m => m.EndDate)%>

I cannot change the display template, which is common to all instances of the Date object, I also do not want to change the model itself. Basically, I want something like:

<%:Html.DisplayFor((m => (m.EndDate == null) ? "No End Date Specified" : m.EndDate)%>

Is this possible in any form? If not, what will be the best way to implement this functionality. I think, even if there is a way to do this, if it is not a good idea, please let me know why not and any better way to do it.

+3
source share
2 answers

Try using UIHint .

[UIHint("CustomDateNull")]
public CustomDate EndDate { get;set; }

Then create a display template CustomDateNull.ascx. Helpers will look for UIHint before returning to the type itself.

If you cannot edit the model at all, you will have to resort to using RenderPartial and passing your date as a model for your partial viewing.

+1
source

, ?

, :/Shared/DisplayTemplates/CustomDate.ascx :/MySpecific/DisplayTemplates/CustomDate.ascx

- DisplayFor. , , , DisplayFor , , . , , , , .

0

Source: https://habr.com/ru/post/1771708/


All Articles