0

I'm struggling with the following problem and I can't find an acceptable way to solve it.

My challenge: write out HTML comments just before the actual property value in a Razor view.

This is my (simplyfied) Viewmodel:

public class Article
{
    public string Title {get;set;}
}

To write out this title I simply do this in my Razor view:

<h2>@Model.Title</h2>

Now I want to write out a html comment just before the actual title so the generated HTML looks like this (simplyfied):

<h2><!-- some parameters for a 3th party system --> This is my title</h2>

The HTML comment comes from an Attribute I applied to the 'Title' attribute. It's value is generated, so the attribute-value is added at runtime using the TypeDescriptor from the .NET framework.

Now I know I could achieve this by simply writing out all my properties using an HTML helper. Like this: @MyHelper.Write(m => m.Title)

But since potentially ALL my properties need this HTML comment I want to avoid the use of an HTML helper since it clutters the View and doesn't make the view look nice and (more) readable.

This is what I have tried:

  • Created a custom Razor base page (Inheriting from WebViewPage<TModel>). And overwriting it's 'Write' method.

This kind of works but the BIGGEST problem here is that I don't know which property is been written out at that moment. There is no way of getting the current property name in the 'Write' method. So now I dynamically search my Model to find a property with the value that's been written out and prepend the HTML comment from the attribute.

My question: is there another approach to accomplish what I want. As sais before: I want to avoid using an HTML helper to write out all my properties. (Think about loops, etc. It's just not nice).

Also, adding this HTML comment in my Controller is no option since:

  1. it's not part of the actual value. Is a sort of metadata.
  2. The HTML comment should be added to int's, double's and DateTime's. There is no way to adjust a double property to include a string. (Image a List<DateTime>. All date's need this HTML comment)
  3. the HTML comment should be added based on a web.config setting. Yes or No. (The actual HTML comment is different for each value of a property)

I realize this question is rather long. Sorry for that. Any thoughts are appreciated.

4
  • Any reason you can't just put this in you _layout shared view? Commented Jan 19, 2013 at 13:41
  • Not sure what you mean with 'this'... Commented Jan 19, 2013 at 13:48
  • The title with the comment. Commented Jan 19, 2013 at 13:48
  • @Oded There's no guarantee of the existence of that particular field on that particular model in a shared layout. Besides, OP is using that as one example: "since potentially ALL my properties need this". Commented Jan 19, 2013 at 13:51

1 Answer 1

1

You can use the existing @Html.Raw(Model.Title)

Alternatively you can use a display templates. Add a UIHintAttribute to the properties you wish to behave this way.

public class MyModel 
{
    [UIHint("Raw")]
    public string MyString { get; set; }
}

Create a new display template called Raw.cshtml that accepts model of type string:

@model string
@Html.Raw(model)

Then in your view you can use:

@Html.DisplayFor(m => m.MyString)

This still requires that you use a helper (DisplayFor). This is a recommended practice that allows you to easily change the behavior of one or many fields with minimal code changes.

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

7 Comments

Wow. I did read about display templates, but didn't realize the potential. Going to give this a try. Looks promising. Will let you know the outcome. Thanks!
@RainyDays. You're absolutely right, there's massive potential in display and editor templates. Once you get to grips with them, you'll never want to do @Model.MyString ever again.
The Raw.cshtml goes into a DisplayTemplates folder in Shared generally.
@manojlds That's a matter of scope. For this particular example, yes. For more context specific templates, no.
Thats why I said generally :)
|

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.