2

アプリケーションの役割を含む文字列配列から要素を取得しようとすると、なぜ例外が発生するのかを理解しようとしています。

エラー:オブジェクト参照がオブジェクトのインスタンスに設定されていません。(ViewBag.roles)

これが私が役割を取得する方法です:

   public ActionResult AdminRolesAndAccessRules()
   {
        String[] rolesArray;
        rolesArray = Roles.GetAllRoles();

        ViewBag.roles = rolesArray;

        return PartialView();
    }

そして、これが私が情報をどのように使用しているかです:

<div class="scroller">
    <table>
        @foreach (MembershipUserCollection muc in ViewBag.roles)
    {                                        
        if(column == 1) {
            classe = "whiteRow";
            column = 0;
        }
        else {
            classe = "grayRow";
            column = 1;
        }
    <tr class="@classe"> 
        <td class="adminSetFormRowUsuario">role</td> 
        <td class="adminSetFormRowGrupo"></td> 
        <td class="formAction centerAlign">
            <img src="~/Images/editar.gif" alt="Editar"/>
            <span style="margin-left:5px"></span>
            <a href='javascript:AjaxRemoveUser("/Account/DeleteUser?role=@muc", "POST", "DinamicContent", "AdminSettingsTabsStructure")'><img src="~/Images/excluir.gif" alt="Excluir"/></a>
        </td> 
    </tr>

    }     
    </table>

デバッグ私は2つの値を見ることができますViewBag.rolesが、それでもシステムはNullまたはインスタンス化されていないオブジェクトについて不平を言っています。

4

1 に答える 1

1
// string array being assigned here
String[] rolesArray = Roles.GetAllRoles();
ViewBag.roles = rolesArray;

デバギングViewBag.rulesに2つの値が表示されますが、それでもシステムはNullまたはインスタンス化されていないオブジェクトについて不平を言っています。

これらの2つの値は、おそらく割り当てた役割です。

// This looks wrong...you are treating each string in the array as MembershipUserCollection
@foreach (MembershipUserCollection muc in ViewBag.roles)
{
}

// instead, this should give you the role names
@foreach( string role in ViewBag.roles )
{
    <div>@role</div>
}
于 2012-11-21T01:00:21.683 に答える