2

In ASP.NET MVC 3 application I am developing I have an enumeration and custom display template for it.

In one of the views I need to display specific enum value, but when I write code like:

@Html.DisplayFor(model => EnumType.EnumValue)

I recieve compilation error:

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

I can think of solution to add property to my model with appropriate value, but maybe there is easier approach to leverage my display template in such scenario?

1 Answer 1

1

You can define custom editor and display templates for types by naming the template for the type.

Information on how templates are identified: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

For Razor replace <%@ Control ... with @model TemplateType.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I have done so. Question is how to use it if I want to display not the Model property by enumeration value (constant). Like, for example @Html.DisplayFor(model => DayOfWeek.Sunday), if I have defined display template for DayOfWeek enum.
@user727030: Sorry, missed the bit about static result. The expression passed to DisplayFor is not evaluated but specific Expression sub-types are used to identify the property or field selected. Have you considered using it as a partial view?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.