views:

63

answers:

2

How can I write the following in MVC?

        <input type="text" name="ProjectList[' + count++ + '].ID"  value = ' + value + ' />
+1  A: 
<%= Html.TextBox("ProjectList[" + (count++) + "].ID", 
    value, new { @class = "css" }) %>
David Liddle
+2  A: 

Use code expressions in <%= ... %> block in the attributes.

 <input type="text" name="<%= ProjectList[count++].ID %>"  value = ' + value + ' />

If the value is meant to be another property of the ProjectList item... then setting a local variable would be easier:

 <% var item = ProjectList[count++].ID; %>
 <input type="text" name="<%= item.ID %>"  value = '<%= item.value %>' />

Albeit that the HTML helpers (see other answer) provides a better approach.

NB. In .NET 4 prefer <%: ... %> to ensure things are HTML Encoded.

Richard
I like this more, the *helpers* are not always *helping*...You should encode it too in .NET 3.5 <%=Html.Encode(item.value)%> and <%=Html.Encode(item.ID)%>
Stephane