-1

私はASP.NET MVCを初めて使用しています。個々のユーザーアカウントを使用したサンプルプロジェクトで作業しています。私は自分のプロジェクトフォルダに新しいエリアを作成し、UserManagementフォルダを含んでいます。 AccountControllerに)レジスタ(に続いてDropDownListFor、ASP.NET MVCでNullReferenceExceptionが発生する

using Library.Areas.UserManagement; 
........ 
........ 
UserManagementMethods userManagement = new UserManagementMethods(); 

、I:AccountControllerで

namespace Library.Areas.UserManagement 
{ 
    public class UserManagementMethods 
    { 
      public IEnumerable<string> GetGenderList() 
      { 
      List<string> GenderGroup = new List<string> 
                 { 
                  "Male", 
                  "Female", 
                  "Not in the Above", 
                 }; 
      return GenderGroup; 

      } 

      public IEnumerable<SelectListItem> GetSelectedGenderItem(IEnumerable<string> elements) 
      { 

       foreach (var element in elements) 
       { 
        selectList.Add(new SelectListItem 
        { 
         Value = element, 
         Text = element 
        }); 
       } 

       return selectList; 
      } 

    } 
} 

、私はUserManagementMethodsオブジェクト、userManagementを作成しました:それは、コードを以下ましたUserManagementMethods.csという名前のファイルが含まれていますUserManagementMethods方法

var genderList = userManagement.GetGenderList(); 
var model = new RegisterViewModel(); 
model.GenderList = userManagement.GetSelectedGenderItem(genderList); 
return View(); 

と呼ばれる私はRegister.cshtml

でDropDownListコントロールを作成しました10
<div class="form-group"> 
    @Html.LabelFor(m => m.Gender, new { @class = "col-md-4 control-label" }) 
    <div class="col-md-8"> 
     @Html.DropDownListFor(m => m.Gender, Model.GenderList, "Select An Option", new { @class = "form-control" }) 
    </div> 
</div> 

私はプロジェクトを実行し、/Account/Registerに移動すると、とNullReferenceExceptionがDropDownListForフィールドの近くに発生します。

/Account/Register

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 49: @Html.LabelFor(m => m.Gender, new { @class= "col-md-4 control-label"})< 
Line 50: <div class="col-md-8"> 
Line 51:  @Html.DropDownListFor(m => m.Gender, Model.GenderList, "Select An Option", new { @class = "form-control" }) 
Line 52: </div> 
Line 53:</div> 
+0

をする必要があります - 'ビュー(モデル)を返します。 –

+0

ありがとうございました。 – PROTOCOL

答えて

0

あなたは、ビューにモデルを送信していません。

return View(); 

ビューでその `null`なのでので、ビューにモデルを返さないので

return View(model); 
+0

ありがとうございました。 – PROTOCOL

関連する問題