MV()的Create()视图中的ArgumentNullException

时间:2014-05-22 16:57:31

标签: c# asp.net-mvc razor

我正在尝试构建Create()视图以添加新人,但是当我到达ArgumentNullException选项时,我会收到DropDownListFor

我的角色表包含三个角色 Actor Director Producer (值1,2,3分别为他们的密钥ID)我希望他们在Create()上填充这个下拉框。

我的朋友让这个用于Edit()函数来更新我的人员数据库,现在我正在尝试为Create()执行此操作。我最初是在Visual Studio中自动生成默认的方法视图创建,编辑,详细信息,删除,索引),但我一直在通过并将它们去除以适应我的自定义信息/查询。 除了Create()方法之外,现在一切正常。

确切的错误是

  

发生了'System.ArgumentNullException'类型的异常   System.Core.dll但未在用户代码中处理

以下是create.cshtml视图,Create()控制器方法和EmployeeDetailModel Viewmodel的一些代码段。

创建()

// GET: /Movies/Create
public ActionResult Create()
{
   var model = new EmployeeDetailModel();
   return View(model);
}

创建(EmployeeDetailModel模型)

由于错误导致我的应用尚未通过“创建视图”,因此不确定此代码是否仍然可用,但我将其放在此处以供参考

// POST
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(EmployeeDetailModel model)
{
   MovieEmployee newMovieEmployee = new MovieEmployee();
   if (ModelState.IsValid)
   {
      newMovieEmployee.roleID = model.NewRoleId;
      newMovieEmployee.employeeID = model.Employee.ID;
      newMovieEmployee.movieID = model.MovieEmployee.movieID;
      Employee newEmployee = new Employee();
      newEmployee.Birthday = model.Employee.Birthday;
      newEmployee.Name = model.Employee.Name;

      db.MovieEmployees.Add(newMovieEmployee);
      db.Employees.Add(newEmployee);

      db.SaveChanges();
      return RedirectToAction("EmployeeInformation");
   }
   return View(model);
}

Create.cshtml - 这是错误发生的地方,@Html.DropDownListFor...

@model WebApplication2.Models.EmployeeDetailModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Movie</h4>
        <hr />
        @Html.ValidationSummary(true)

        @Html.HiddenFor(model => model.Employee.ID)
        @Html.HiddenFor(model => model.MovieEmployee.roleID)
        @Html.HiddenFor(model => model.MovieEmployee.movieID)
        @Html.HiddenFor(model => model.MovieEmployee.employeeID)

        <div class="form-group">
            @Html.LabelFor(model => model.Employee.Name, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Employee.Name)
                @Html.ValidationMessageFor(model => model.Employee.Name)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Roles, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(r => r.NewRoleId, Model.Roles.Select(x => new SelectListItem() { Selected = (x.ID == Model.MovieEmployee.roleID), Text = x.RoleType, Value = x.ID.ToString() }))
                @Html.ValidationMessageFor(model => model.NewRoleId)
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.Employee.Birthday, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Employee.Birthday)
                @Html.ValidationMessageFor(model => model.Employee.Birthday)
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "EmployeeInformation")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

EmployeeDetailModel

namespace WebApplication2.Models
public class EmployeeDetailModel
    {
        public WebApplication2.Entities.Employee Employee { get; set; }
        public WebApplication2.Entities.MovieEmployee MovieEmployee { get; set; }
        public IEnumerable<WebApplication2.Entities.Role> Roles { get; set; }
        public int NewRoleId { get; set; }
    }

我还处于MVC的早期学习阶段,所以非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

问题在于

Model.Roles.Select

在模型中,Roles没有初始化为任何东西,因此当然是空的。当它传递给扩展方法Select时,会抛出错误。

您需要在模型类的构造函数中或在调用return View(model);之前的操作中初始化此集合。所以:

public class EmployeeDetailModel
{
    public WebApplication2.Entities.Employee Employee { get; set; }
    public WebApplication2.Entities.MovieEmployee MovieEmployee { get; set; }
    public IEnumerable<WebApplication2.Entities.Role> Roles { get; set; }
    public int NewRoleId { get; set; }

    public EmployeeDetailModel()
    {
        Roles = new List<WebApplication2.Entities.Role>();
    }
}

public ActionResult Create()
{
    var model = new EmployeeDetailModel();
    model.Roles = new List<WebApplication2.Entities.Role>();
    return View(model);
}