Html.Label to display email address

I am using Html.Label to display some data in my MVC application. When I try to display the email address, it does not display correctly

Model.CopyToEmail = lab@gmail.com @Html.Label((string)(Model.CopyToEmail)) 

but the processed output that I see is

 com 

Can anyone suggest how to render using Html.Label

 lab@gmail.com 
+6
source share
3 answers

Html.Label takes a field to display the <label> for.
It reads your string as a nested property and prints the name of the innermost property.

You just want to write plain text:

 @Model.CopyToEmail 

You can also call @Html.DisplayFor(m => m.CopyToEmail) .

+12
source

Using strongly typed views (passing views in views rather than using ViewBag / ViewData) is good practice and allows you to use common overloads of html helpers. Here is your example rewritten with a strongly typed html helper

 Html.LabelFor(model => model.CopyToEmail) 

And the shortcut in html does not exist to display the data, there is a label property, editable. You can use [DisplayAttribute] in the property or use this overload

 public static MvcHtmlString Label( this HtmlHelper html, string expression, string labelText ) 

//

 Html.LabelFor(model => model.CopyToEmail, Model.CopyToEmailLabelText) 
+1
source

Can you take a look at the model OR the data source on the view to return the value of the property: for example, Html.Encode(Model.Email)

+1
source

Source: https://habr.com/ru/post/920818/


All Articles