0

I have this line of code within my .aspx file

<label title="<%= Model.ProductName %>"></label>

why is it when I run it, the label is not showing at all.

yet if I do something like this it would work:

<%: Html.LabelFor(model => model.ProductName) %>

I would really like for the first method to work, is there a way?

thank you

bzlm
9,7476 gold badges70 silver badges92 bronze badges
asked Jul 2, 2011 at 17:18
1
  • 1
    go read up on the label HTML element. :) Commented Jul 2, 2011 at 17:22

1 Answer 1

2

It's because you need to provide contents for this label:

<label title="some title" for="ProductName">
 <%: Model.ProductName %>
</label>

The way you wrote your markup the <label> tag is empty. Also make sure you properly HTML encode the contents. Notice in my example the usage of <%: (available only in ASP.NET 4) instead of <%=. If you are running on previous versions you could use the the following:

<label title="some title" for="ProductName">
 <%= Html.DisplayFor(x => x.ProductName) %>
</label>
answered Jul 2, 2011 at 17:21

3 Comments

Thanks for your help, there is another problem, is there a way to set the width for <%: Html.TextBoxFor(model => model.Synopsis, new { @class = "wide" })%> I tried adding the class in the css file, but didn't work
@Chen Lu, in your separate CSS file you could try defining the wide rule like this: .wide { width: 200px; }.
It worked out this way actually: <%: Html.TextBoxFor(model => model.Synopsis, new {style="width:300px"} )%> Thanks for your help!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.