views:

50

answers:

1

Hi, I have the code (snippet):

The Model is the IEnumerable object of the Person's class:

<% foreach (var item in Model) 
  { %>
     <tr>
         <td><%= Html.DisplayFor(x=>item.Name) %></td>
     </tr>    
<% } %>

it renders only labels like that:

<td>Tommy</td>

According to the link it should be rendering a HTML markup something like:

alt text

but there is no the ID and the NAME property. Why ?

+1  A: 

Your using the wrong template your should be using Html.EditorFor(x => x.Name)

Edit: I said you were using the wrong template because in your image it is a textbox displayed, not a label...

the default ouptut of Displayfor is

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Encode(ViewData.TemplateInfo.FormattedModelValue) %>

according to Brad Wilson. You could easily build your own, look the other post of Brad Wilson for examples.

Or you could simply call Html.LabelFor(x => x.Name)

If you always want that, add a template, name String.ascx in your Views/Share/DisplayTemplate and just put the following in :

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.Label("", ViewData.TemplateInfo.FormattedModelValue) %>
moi_meme
why is it wrong ? It should be a label, not a texbox
Tony
@Tony see my edit...
moi_meme