Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from model for Html.Label?

Tags:

I want to place the value of a field (property) from model into Html.Label. Something like this:

@Html.Label(item => item.Title)

I don't want a label for item.Title (like Html.LabelFor( model => model.Title)). But I want to put the value of item.Title in the label as text (string). So the result in run-time should be like this:

<label>Some Title</label>

How can I do this?

like image 586
Rahmani Avatar asked Feb 24 '14 13:02

Rahmani


People also ask

How do I get text from labels in HTML?

Use the textContent property to get the text of a label element, e.g. const text = label. textContent . The textContent property will return the text content of the label and its descendants.

How do you bind data in a label in HTML?

A data-* attribute on a <label> tag attaches additional data to the label. To create a custom attribute, replace * with a lowercase string, such as data-id , data-status , or data-location .

What is HTML LabelFor?

LabelFor() The Html. LabelFor<TModel,TProperty>() helper method is a strongly typed extension method. It generates a html label element for the model object property specified using a lambda expression.

Which attribute can be applied to a property of a model to specify the label name in a view?

The label is specified by using the Label Attribute applied to the property.


2 Answers

Try this:

@Html.Label(Model.Title)

It should work

EDITED

or this:

<label>@Html.DisplayFor(item => item.Title)</label>
like image 166
melvas Avatar answered Oct 05 '22 05:10

melvas


<label>@Model.Title</label>
like image 28
André Figueiredo Avatar answered Oct 05 '22 03:10

André Figueiredo