ASP.Net MVC DropDownList HttpPost Error: Value cannot be null. Parameter name: items

Last Reply 2 months ago By dharmendr

Posted 2 months ago

HI everyone,

I am working in MVC4 Razor I have created a form where i bind the dropdown with Database dropdown successsfully bind the data but when I submit the data then 

It shows me error - Value cannot be null.Parameter name: items

Model:

public class DepartmentModel
{
    public string deptId { get; set; }
    public string deptName { get; set; }
}

View :

@using (Html.BeginForm("alumniSurvey", "important_link", FormMethod.Post))
{
<div class="form-group">
    @{
        @Html.DropDownList("department", new SelectList(ViewBag.deptList, "deptId", "deptName"), "-- Select Department --", new { @class = "form-control" })
        @Html.Hidden("hid_department")
        @Html.Hidden("hid_course")
    }
</div>
}

the form is too long so button is not showing here there is input button in my design page I add ony dropdowlist design code

Controller:-

private List<Models.DepartmentModel> bindDepartment()
{
    List<Models.DepartmentModel> deptList = new List<Models.DepartmentModel>();
    SqlCommand com1 = new SqlCommand("sp_bindDepartment", con);
    com1.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da1 = new SqlDataAdapter(com1);
    DataTable ds1 = new DataTable();
    da1.Fill(ds1);
    for (int i = 0; i < ds1.Rows.Count; i++)
    {
        Models.DepartmentModel dm = new Models.DepartmentModel();
        dm.deptId = ds1.Rows[i]["Id"].ToString();
        dm.deptName = ds1.Rows[i]["deptName"].ToString();
        deptList.Add(dm);
    }
    return deptList;
}

public ActionResult alumniSurvey()
{
    ViewBag.deptList = bindDepartment();
    return View();
}
[HttpPost]
public ActionResult alumniSurvery(FormCollection frm)
{
    return View();
}

Please give any suggestion

Posted 2 months ago

Hi pankaj04,

In the Post method before returning to the View() assign the ViewBag.deptList value again.

[HttpPost]
public ActionResult alumniSurvery(FormCollection frm)
{
    ViewBag.deptList = bindDepartment();
    return View();
}

Check the below example.

Model

public class DepartmentModel
{
    public string deptId { get; set; }
    public string deptName { get; set; }
}

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    private List<Models.DepartmentModel> bindDepartment()
    {
        List<Models.DepartmentModel> deptList = new List<Models.DepartmentModel>();
        Models.DepartmentModel dm = new Models.DepartmentModel();
        dm.deptId = "1";
        dm.deptName = "Computer";
        deptList.Add(dm);

        dm = new Models.DepartmentModel();
        dm.deptId = "2";
        dm.deptName = "IT";
        deptList.Add(dm);

        return deptList;
    }

    public ActionResult alumniSurvey()
    {
        TempData["deptList"] = bindDepartment();
        return View();
    }

    [HttpPost]
    public ActionResult alumniSurvey(FormCollection frm)
    {
        string department = frm["department"];
        TempData["SelectedDepartment"] = department;
        TempData["deptList"] = bindDepartment();
        return View();
    }
}

View

<div>
    <%using (Html.BeginForm("alumniSurvey", "Home", FormMethod.Post)) { %>
    <div class="form-group">
        <%{%>
        <%:Html.DropDownList("department", new SelectList((List<DepartmentModel>)TempData["deptList"], "deptId", "deptName"), "-- Select Department --", new { })%>
        <%:Html.Hidden("hid_department")%>
        <%:Html.Hidden("hid_course")%>
        <%} %>
    </div>
    <input type="submit" value="Save" />
    <br />
    <%} %>
    <%if (TempData["SelectedDepartment"] != null) {%>
    <%=TempData["SelectedDepartment"] %>
    <%  } %>
</div>

Screenshot