ASP.NET MVC - checkbox text attribute
I am drawing some checkboxes in a loop, and I want to set a text attribute based on the objects that I am iterating with the loop.
I have something like this:
<asp:CheckBox ID=
"CheckBox1 " runat=
" server " Text=
" <%= Html.Encode(item.nome) %>
" Checked = "true" / ">
The problem is that Html.Encode (item.nome) appears as plain text and if I don't use quotes I get an error.
a source to share
You cannot mix ASP.NET control tags with syntax <%= %>
. You have two options:
Use raw HTML for your checkbox, then you can use <%= %>
just fine. This style is better suited for ASP.NET MVC.
<input type="checkbox" name="cb" checked="checked"><%= Html.Encode(item.nome) %></input>
Or you can use ASP.NET compatible data binding syntax:
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Html.Encode(Container.DataItem, "nome") %>' Checked="true"/>
But to use the data binding syntax, you need a data source control and be inside a Repeater control. See ASP.NET Data Binding for details .
a source to share