NullReferenceException 出现在 DropDownListFor 中,ASP.NET MVC
NullReferenceException occurs in DropDownListFor , ASP.NET MVC
我是 ASP.NET 的新手 MVC.I 正在使用个人用户帐户处理示例项目。我在我的项目文件夹中创建了一个新区域,它包含 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 中,我创建了一个 UserManagementMethods 对象,userManagement :
using Library.Areas.UserManagement;
........
........
UserManagementMethods userManagement = new UserManagementMethods();
然后在 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>
当我 运行 项目并导航到 /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>
您没有将模型发送到视图。
return View();
应该是
return View(model);
我是 ASP.NET 的新手 MVC.I 正在使用个人用户帐户处理示例项目。我在我的项目文件夹中创建了一个新区域,它包含 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 中,我创建了一个 UserManagementMethods 对象,userManagement :
using Library.Areas.UserManagement;
........
........
UserManagementMethods userManagement = new UserManagementMethods();
然后在 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>
当我 运行 项目并导航到 /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>
您没有将模型发送到视图。
return View();
应该是
return View(model);