Question

I created a display template in Razor specifically for displaying currency, which I now want to remove in my code and replace with a standard text display template that accepts a string format (which I can set to "C").

There are a lot of occurrences however so I would like to use something like the [Obsolete] attribute with a warning message to allow this to be done over the course of the next couple of weeks without breaking all the code.

Is this possible or is there an equivalent method?

Cheers,

Adam.

Was it helpful?

Solution

Seemingly not :(

The DisplayTemplates are generic, eg.:

@Html.DisplayFor(model => model.Foo)

To use [Obsolete], you'd have to mark the DisplayFor method, which would apply to them all. I tried to create a "more specific" version, to see if it would pick that up (since you could then mark that as [Obsolete], like this:

public static class MyExtensions
{
    [Obsolete]
    public static MvcHtmlString DisplayFor<TModel, DateTime>(this HtmlHelper<TModel> html, Expression<Func<TModel, DateTime>> expression)
    {
        return html.DisplayFor(expression);
    }
}

Sadly, this didn't work - it gives the following error:

Compiler Error Message: CS0121: The call is ambiguous between the following methods or properties: 'MvcApplication11.Models.MyExtensions.DisplayFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' and 'System.Web.Mvc.Html.DisplayExtensions.DisplayFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)'

So I don't think you can achieve what you need with [Obsolete]. If you really can't pick through the calls to DisplayFor in the IDE, you could try logging the stack trace inside the template (since you can execute any C# inside it) and then running through the app. This won't be bullet-proof, but might help a little.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top