-1

I am new to ASP.NET MVC.I am working with a sample Project using Individual User Accounts. I created a new Area in my project folder and it contains UserManagement Folder. It contains a file named UserManagementMethods.cs which has following code :

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

    }
}

In the AccountController, I created a UserManagementMethods object, userManagement :

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

Then in the Register() in the AccountController, I called UserManagementMethods methods

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

I created a DropDownList in Register.cshtml

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

When I run the project and navigate to /Account/Register, NullReferenceException occurs near to DropDownListFor Field.

/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>
PROTOCOL
  • 367
  • 10
  • 16
  • Because you do not return the model to the view so its `null` in the view - `return View(model); –  Feb 20 '17 at 05:30

1 Answers1

1

You aren't sending the model to the view.

return View();

should be

return View(model);