Mvc中使用Dropdown List时出现Argument null Exception问题求助
Hey there! Let's break down why you're hitting that Value cannot be null. Parameter name: items error, even though your data successfully inserts into the database. The core issue is that the ViewBag.App value being passed to your dropdown is null when the view renders—let's fix this step by step.
1. First: Fix the Data Source to Never Return Null
The error likely starts with your db.UserSelectApprover() method. If this method returns null (instead of an empty collection when there are no approvers), it will make your items variable null, which then gets stored in ViewBag.App.
Update your controller code to ensure you always have a valid collection:
// Replace YourEntityType with the actual type returned by UserSelectApprover() var ob = db.UserSelectApprover() ?? new List<YourEntityType>(); IEnumerable<SelectListItem> items = ob.Select(c => new SelectListItem { Value = c.userid.ToString(), Text = $"{c.firstname} {c.lastname}" }).ToList(); ViewBag.App = items;
The ?? new List<YourEntityType>() ensures that even if UserSelectApprover() returns null, you'll have an empty (but non-null) list to work with.
2. Simplify Your DropDownListFor Call
Right now you're wrapping ViewBag.App (which is already an IEnumerable<SelectListItem>) in a new SelectList—this is unnecessary, and it's one more place where a null value can cause an exception.
Update your view code to use the ViewBag.App directly:
@using (Html.BeginForm("Apply", "Employee", FormMethod.Post, new { @class = "f" })) { ... @Html.DropDownListFor(m => m.Approver, ViewBag.App as IEnumerable<SelectListItem>, "Select Approver", new { @class = "inp1" } ) ... }
This skips the redundant SelectList wrapper and uses the pre-built list items directly.
3. Don't Forget to Reinitialize ViewBag.App in Post Actions
You mentioned the data inserts successfully, which means your POST Apply method is working—but if that method returns the view (e.g., on model validation failure), you need to re-populate ViewBag.App! HTTP is stateless, so the original ViewBag values from the GET request won't persist after a POST.
Here's how to update your POST controller method:
[HttpPost] public ActionResult Apply(YourModel model) { if (ModelState.IsValid) { // Your existing database insert logic here db.SaveChanges(); // Redirect to a success page to avoid duplicate form submissions return RedirectToAction("ApplicationSuccess"); } // If validation fails (or you need to return the view), re-populate ViewBag.App! var ob = db.UserSelectApprover() ?? new List<YourEntityType>(); ViewBag.App = ob.Select(c => new SelectListItem { Value = c.userid.ToString(), Text = $"{c.firstname} {c.lastname}" }).ToList(); return View(model); }
Redirecting on success is a best practice here—it prevents users from accidentally resubmitting the form by refreshing the page.
Quick Recap of Key Fixes
- Always return an empty collection instead of null from your EF queries
- Avoid wrapping pre-built
SelectListItemcollections inSelectList - Re-populate dropdown data sources when returning a view from a POST action
内容的提问来源于stack exchange,提问作者abc

