我目前正在从事这项任务。除非我从另一个表中放入一个下拉列表,否则一切正常。 我需要在我的数据库中插入一行,但我遇到了一个错误
System.ArgumentNullException: 'Value cannot be null.
Parameter name: items'
我已经找到了我在网上找到的解决方案,但出于某些原因它并不适用于我的问题。
控制器:
[HttpGet]
public ActionResult Add()
{
try
{
ViewBag.Departments = db.Departments.ToList();
return View();
}
catch (Exception genericException)
{
ViewBag.ExceptionMessage = genericException.Message;
}
return View("~/Views/Errors/Details.cshtml");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Add(Volunteer volunteer)
{
try
{
if (ModelState.IsValid)
{
db.Volunteers.Add(volunteer);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Department = db.Departments.ToList();
return View(volunteer);
}
catch (DbUpdateException DbException)
{
ViewBag.DbExceptionMessage = DbException.Message;
}
catch (Exception genericException)
{
ViewBag.ExceptionMessage = genericException;
}
return View("~/Views/Errors/Details.cshtml");
}
查看:
@model Features.Models.Volunteer
@{
ViewBag.Title = "Add";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Add a volunteer</h2>
@using (Html.BeginForm("Add", "Volunteer"))
{
@Html.AntiForgeryToken()
<div>
<div>
@Html.LabelFor(v => v.Department)
@Html.DropDownListFor(v => v.Department, new SelectList(ViewBag.Department, "DepartmentName", "DepartmentName"), "-")
@Html.ValidationMessageFor(v => v.Department)
</div>
</div>
<input type="submit" value="Create" />
是的,有人能帮帮我吗?谢谢。