我試圖構建一個Create()
視圖來添加新的人,但我得到ArgumentNullException
時,它得到DropDownListFor
選項。ArguementNullException MVC中的創建()視圖
我角色表有三個角色演員,主任和生產者(分別價值1,2,3他們的密鑰ID),我希望他們以填充Create()
這個dropdownbox。
我的朋友得到了這個工作的Edit()
函數來更新我的數據庫的人,現在我正在嘗試做Create()
。我原本在Visual Studio中自動生成默認的方法和視圖(創建,編輯,詳細信息,刪除,索引),但我一直在經歷和消除它們以適應我的自定義信息/查詢。 現在一切正常,除了我的Create()
方法。
確切的錯誤是
類型「System.ArgumentNullException」發生在 System.Core.dll的一個例外,但在用戶代碼中沒有處理
下面是一些代碼段create.cshtml
查看,Create()
控制器方法和我的EmployeeDetailModel
Viewmodel。
的Create()
// 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的早期學習階段,所以任何幫助不勝感激!
,在構造函數中做什麼聰明嗎?如果沒有,那麼在你的視圖中像'Model.Roles.Select'和'Model.MovieEmployee.roleID'這樣的代碼將拋出'NullReferenceException'。 – DavidG