Html.Label to display email address

妖精的绣舞 提交于 2019-11-27 06:56:27

问题


I am using Html.Label to display some data in my MVC applilcation. When I am trying to display email address it does not render properly

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

but the rendered output that i see is

com

Could anyone please suggest how to display using Html.Label

lab@gmail.com 

回答1:


Html.Label takes a field to display a <label> tag for.
It's reading your string as a nested property, and printing the name of the innermost property.

You just want to write ordinary text:

@Model.CopyToEmail

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




回答2:


Using strongly typed views (passing viewmodels to views, and not using ViewBag/ViewData) is good practice, and allows you to use generic overloads of html helpers. Here is your example rewritten using strongly typed html helper

Html.LabelFor(model => model.CopyToEmail)

And label in html is not there to display data, it is there to label property being edited. You could use [DisplayAttribute] on your property, or use this overload

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

//

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



回答3:


Can you please look at the model OR your data source to the view to return the property value: i.e. like, Html.Encode(Model.Email)



来源:https://stackoverflow.com/questions/11547631/html-label-to-display-email-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!