Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 无法转换类型';char';至';一串_Asp.net Mvc_String - Fatal编程技术网

Asp.net mvc 无法转换类型';char';至';一串

Asp.net mvc 无法转换类型';char';至';一串,asp.net-mvc,string,Asp.net Mvc,String,我在一个项目中遇到了这个错误,代码在另一个项目中运行得非常好。我尝试了多次复制代码。有没有办法找到错误的来源 @if (ViewBag.RolesForThisUser != null) { <div style="background-color:lawngreen;"> <table class="table"> <tr> <th>

我在一个项目中遇到了这个错误,代码在另一个项目中运行得非常好。我尝试了多次复制代码。有没有办法找到错误的来源

@if (ViewBag.RolesForThisUser != null)
{
    <div style="background-color:lawngreen;">
        <table class="table">
            <tr>
                <th>
                    @Html.DisplayName("Roles For This User")
                </th>
            </tr>
            <tr>
                <td>
                  @foreach (string s in ViewBag.RolesForThisUser) //this line
                    {
                        <li>@s</li>
                    }
                </td>
            </tr>
        </table>
@if(ViewBag.RolesForThisUser!=null)
{
@Html.DisplayName(“此用户的角色”)
@foreach(ViewBag.RolesForThisUser中的字符串s)//此行
{
  • @
  • }
    我怀疑
    查看包。该用户的角色本身已经包含一个
    字符串
    ,既不是数组也不是字符串集合(例如
    字符串[]
    列表
    ),因此使用
    foreach
    循环是毫无意义的(而
    字符串
    本身包含
    字符[]
    array,这解释了类型转换失败的原因)。您只需显示它,而无需使用
    foreach

    @if (!string.IsNullOrEmpty(ViewBag.RolesForThisUser))
    {
        <div style="background-color:lawngreen;">
            <table class="table">
                <tr>
                    <th>
                        @Html.DisplayName("Roles For This User")
                    </th>
                </tr>
                <tr>
                    <td>
                       @ViewBag.RolesForThisUser
                    </td>
                </tr>
            </table>
        </div>
    }
    
    查看

    @if (ViewBag.RolesForThisUser != null)
    {
        <div style="background-color:lawngreen;">
            <table class="table">
                <tr>
                    <th>
                        @Html.DisplayName("Roles For This User")
                    </th>
                </tr>
                <tr>
                    <td>
                        @foreach (string s in ViewBag.RolesForThisUser)
                        {
                            <p>@s</p>
                        }
                    </td>
                </tr>
            </table>
        </div>
    }
    
    @if(ViewBag.RolesForThisUser!=null)
    {
    @Html.DisplayName(“此用户的角色”)
    @foreach(ViewBag.rolesforthis用户中的字符串s)
    {
    @

    } }
    @if (ViewBag.RolesForThisUser != null)
    {
        <div style="background-color:lawngreen;">
            <table class="table">
                <tr>
                    <th>
                        @Html.DisplayName("Roles For This User")
                    </th>
                </tr>
                <tr>
                    <td>
                        @foreach (string s in ViewBag.RolesForThisUser)
                        {
                            <p>@s</p>
                        }
                    </td>
                </tr>
            </table>
        </div>
    }