ASP.NET Display Templates - No Output

I have this model:

public class DefinitionListModel : List<DefinitionListItemModel> { } 

Then I have this template setting (partial view) for it in DisplayTemplates:

 @model Company.Product.Models.DefinitionListModel <dl> @foreach (var definition in Model) { Html.DisplayFor(m => definition); } </dl> 

What causes this default display template because each element is a DefinitionListItemModel :

 @model Company.Product.Models.DefinitionListItemModel @Html.DisplayFor(m => m.Term, "DefinitionTerm"); @Html.DisplayFor(m => m.Description, "DefinitionDescription"); 

And the DefinitionTerm template is as follows:

 @model object @{ if (Model != null) { string s = Model as string; if (s != null) { <dt>@s</dt> } else { // Other types and models that could be used. // Last resort. <dt>@Model.ToString()</dt> } } } 

The breakpoint located in the last template has been successfully deleted, and the presentation and layout are displayed only in order, but none of them display, even the DL tag from the first template above.

Why does it hit breakpoints but doesn't display HTML?

0
asp.net-mvc razor asp.net-mvc-5
Apr 24 '14 at 19:44
source share
2 answers

It was all connected with half-columns. It seems I can't just call Html.DisplayFor like a regular C # method with a semicolon.

For example, the DefinitionListModel template has been changed here:

 @model AcmeCo.Hudson.Models.DefinitionListModel @if (Model != null) { <dl> @foreach (var definition in Model) { @Html.DisplayFor(m => definition) } </dl> } 

Note the weird use of @ inside the code block, which is already C #. I assume that there is a correct way to make DisplayFor from within a C # block, but I don't know what it is.

+1
Apr 24 '14 at 19:59
source share

This is not due to the semicolon, this is due to the lack of @ . This is how Razor works. That between braces should not be C# . The Razor parser is trying to figure out what code and what content. To start a piece of code, you must set the @ symbol. The Razor parser identifies everything between curly braces as content.

Check out Scott's blog for some very good examples.

0
Apr 24 '14 at 20:16
source share



All Articles