What is the point of Html.DisplayTextFor ()?

Is there any good reason to use a strongly typed html helper ...

<%: Html.DisplayTextFor(model => model.Email) %> 

Unlike...

 <%: Model.Email %> 
+44
asp.net-mvc html-helper
Aug 13 2018-10-10T00:
source share
3 answers

Consider the following model:

 public class MyModel { public string Name { get; set; } [DisplayFormat(NullDisplayText = "No value available!")] public string Email { get; set; } } 

in my opinion:

 <%= Html.DisplayTextFor(m => m.Email) %> <%: Model.Email %> 

The first line will display β€œNo Value” if we leave the message β€œnull” and the second line will not display anything.

Conclusion: Html.DisplayTextFor considers DataAnnotations for your properties, <%: Model.Email %> will not. In addition, <%: Model.Email %> will cause an "object reference error" when the value is null, but <%= Html.DisplayTextFor %> will not.

+70
Aug 13 '10 at 10:30
source share

DisplayTextFor will also be called during execution of "DisplayFor" and "EditFor". This ensures that any template helpers will display the text using the correct template helpers if they are installed ... therefore changing a single template assistant will spread across all displays of this text element ... simple display, edit form, create forms, etc. . and etc.

+6
Aug 13 2018-10-10T00:
source share

Well, DisplayTextFor won't break unless you pass in the model (or pass null). Model.Email throws an exception in null. Thus, DisplayTextFor is more reliable.

The strongly typed helpers point allows you to increase compilation check time. It is very convenient when refactoring.

DisplayTextFor allows you to use a consistent design (with other highly typed helpers) throughout the page. Some people may find this more attractive.

DisplayTextFor also lets you pass template and field names as parameters.

+5
Aug 13 2018-10-10T00:
source share



All Articles