Here’s the source:
<% string[] roles = ViewData["Roles"] as string[];
if (roles != null && roles.Length > 0) {%>
<p>
<label for="roleName">
Role:</label>
<% foreach (string role in roles) { %>
<%: Html.RadioButtonFor(m => m.RoleName, role) %> <span>
<%: role%></span>
<% } %>
</p>
<%} %>
Here’s my attempt:
@{
string[] roles = ViewData["Roles"] as string[];
if (roles != null && roles.Length > 0) {
<p>
<label for="roleName">Role:</label>
foreach (string role in roles) {
@Html.RadioButtonFor(m => m.RoleName, @role) <span>@role</span>
}
</p>
}
}
The problem is that at run time I get the following error message:
Compiler Error Message: CS0103: The name 'role' does not exist in the current context
Source Error:
Line 41: <label for="roleName">Role:</label>
Line 42: foreach (string role in roles) {
Line 43: @Html.RadioButtonFor(m => m.RoleName, @role) <span>@role</span>
Line 44: </p>
Line 45: }
Can someone see what’s wrong. I tried but it seems there is something wrong with my attempt. So far I also tried change line 43 to:
@Html.RadioButtonFor(m => m.RoleName, role) <span>@(role)</span>
@Html.RadioButtonFor(m => m.RoleName, role) <span>@role</span>
Both still don’t work 🙁
should be