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>
}