这是来源:
<% 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>
<%} %>
这是我的尝试:
@{
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>
}
}
问题是在运行时我收到以下错误消息:
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: }
有人可以看看有什么问题。我试过了,但我的尝试似乎有问题。到目前为止,我还尝试将第 43 行更改为:
@Html.RadioButtonFor(m => m.RoleName, role) <span>@(role)</span>
@Html.RadioButtonFor(m => m.RoleName, role) <span>@role</span>
两者仍然不起作用:-(