1

I am new to Mvc

I get the following error in the browser window when i used ef to bind data to dropdownlist

(Error):argument null exception was unhandled by user code and value Can't be null

Controller:

var ob = db.UserSelectApprover();
IEnumerable<SelectListItem> items = ob.Select(c => new SelectListItem
{
Value = c.userid.ToString(),
Text = c.firstname + " " + c.lastname}).ToList();
ViewBag.App = items;

View:

i used dropdown list inside table tag

@using (Html.BeginForm("Apply", "Employee", FormMethod.Post, new { @class = "f" }))
{
    ...
    @Html.DropDownListFor(m=>m.Approver, new SelectList(ViewBag.App, "Value", "Text"),"Select Approver", new { @class = "inp1" } )  
    ...
}

I am getting error as

Server Error in '/' Application.
Value cannot be null.
Parameter name: items
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.ArgumentNullException: Value cannot be null. Parameter name: items

All the fields including selected item from dropdown list was inserted into database despite of the error

abc
  • 19
  • 1
  • Firstly, `ViewBag.App` is already `IEnumerable` so using `new SelectList()` in the view to create another identical `IEnumerable` is pointless. Its just `@Html.DropDownListFor(m=>m.Approver, (IEnumerable)ViewBag.App, "Select Approver", new { ... })` –  May 14 '18 at 23:05
  • And the error means that `ViewBag.App` is `null`, so I assume this is happening when you submit the form and return the view because `ModelState` is invalid but did not repopulate `ViewBag.App` (as you did in the GET method. Suggest you also read [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) –  May 14 '18 at 23:07

0 Answers0