views:

1040

answers:

3

Hi,

I'm using MVC2, Preview 2. Why is it that when I use:

        <%= Html.LabelFor(x => x.Nombres) %>
        <%= Html.Encode(Model.Nombres) %>

It outputs:

Nombre:
Sr. Fulano de Tal

But when I use:

        Nombres:
        <%= Html.Encode(Model.Nombres) %>

it outputs:

Nombre: Sr. Fulano de Tal

I don't want the return after the label. Is it my CSS that is ruining things, or is it the HTML.LabelFor that is producing the extra return.

A: 

View Source will help you know what the right answer is here. What is the HTML generated in each case?

I suspect that what's happening here is that the HTML element generated by Html.LabelFor is defined, in your CSS, as a block element, meaning that subsequent content will start on a new line. If this is what's happening, you can change the CSS for those elements to use display:inline; and they should show up on the same line as your field's value. You can also use the float CSS attribute to generate the same effect, but floats are generally harder to work with than display:inline.

Justin Grant
+3  A: 

This is really hard to answer. Could do with seeing the actual html markup it is producing. I suspect the labels are either floated or display:block in the css.

John Polling
you are right, this has nothing to do with the Html.LabelFor method... It's with the CSS.
hminaya
just a quick note. Label tags should only be used in conjunction with input elements. http://www.w3schools.com/tags/tag_label.asp
John Polling
A: 

The HTML output for your case should be something like: <label for="Nombres">Nombre</label>. No <br/> is rendered, so the only answer should lie in the CSS, like Justin said. Look for css styles attached to "label" elements, or not so probable, something related to "Nombres".

Carlos Fernandes