Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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
C# 在DropDownListFor、ASP.NET MVC中发生NullReferenceException_C#_Asp.net Mvc_Model View Controller_Nullreferenceexception_Html.dropdownlistfor - Fatal编程技术网

C# 在DropDownListFor、ASP.NET MVC中发生NullReferenceException

C# 在DropDownListFor、ASP.NET MVC中发生NullReferenceException,c#,asp.net-mvc,model-view-controller,nullreferenceexception,html.dropdownlistfor,C#,Asp.net Mvc,Model View Controller,Nullreferenceexception,Html.dropdownlistfor,我是ASP.NET MVC新手。我正在使用个人用户帐户处理一个示例项目。我在我的项目文件夹中创建了一个新区域,其中包含UserManagement文件夹。它包含名为UserManagementMethods.cs的文件,该文件具有以下代码: namespace Library.Areas.UserManagement { public class UserManagementMethods { public IEnumerable<string&

我是ASP.NET MVC新手。我正在使用个人用户帐户处理一个示例项目。我在我的项目文件夹中创建了一个新区域,其中包含UserManagement文件夹。它包含名为UserManagementMethods.cs的文件,该文件具有以下代码:

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

    }
}
然后在AccountController的Register()中,我调用了UserManagementMethods方法

var genderList   = userManagement.GetGenderList();
var model = new RegisterViewModel();
model.GenderList = userManagement.GetSelectedGenderItem(genderList);
return View();
我在Register.cshtml中创建了一个DropDownList

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

您没有将模型发送到视图

return View();
应该是

return View(model);

因为您没有将模型返回到视图中,所以它的
null
在视图中-`return view(model);
return View();
return View(model);