<% 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>
雙方仍然不工作:-(
對不起,但這給出了相同的錯誤消息: 42行:foreach在角色串角色){ 第43行:@ Html.RadioButtonFor(M => m.RoleName,角色) @Role 44行:} –
因爲你沒有把@在foreach之前,我對我的回答 – CallumVass
聲明剛剛找到解決方案。我在foreach之前需要一個@ –