简体   繁体   English

如何将这个小代码段从aspx转换为MVC Razor

[英]How can I convert this small code snippet from aspx to MVC Razor

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) %>&nbsp; <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)&nbsp; <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)&nbsp; <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: 到目前为止,我还尝试将第43行更改为:

@Html.RadioButtonFor(m => m.RoleName, role)&nbsp; <span>@(role)</span>

@Html.RadioButtonFor(m => m.RoleName, role)&nbsp; <span>@role</span>

Both still don't work :-( 两者仍然不起作用:-(

@Html.RadioButtonFor(m => m.RoleName, @role)&nbsp; <span

should be 应该

@Html.RadioButtonFor(m => m.RoleName, role)&nbsp; 

the @role is correct. @role是正确的。 but no @ sign is required in the RadioButtonFor(...) call 但在RadioButtonFor(...)调用中不需要@符号

foreach (string role in roles) {    
    @Html.RadioButtonFor(m => m.RoleName, @role)&nbsp; <span>@role</span>
}

should be 应该

@foreach (string role in roles) {    
    @Html.RadioButtonFor(m => m.RoleName, role) <span>@role</span>
}

try this: 尝试这个:

@{
    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)@:&nbsp;<span>@role</span>
        }
    </p>
    }
}

or you can use: 或者您可以使用:

@{
    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)@:&nbsp;<span>@role</span>
    }
</p>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM