Hector Minaya
Hector Minaya

Reputation: 1705

Html.LabelFor helper in MVC2

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.

Upvotes: 2

Views: 2504

Answers (3)

Carlos Fernandes
Carlos Fernandes

Reputation: 160

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".

Upvotes: 0

John Polling
John Polling

Reputation: 2282

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.

Upvotes: 4

Justin Grant
Justin Grant

Reputation: 46663

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.

Upvotes: 0

Related Questions