Search code examples
model-view-controllerhtml.dropdownlistforselectlistasp.net-apicontrollerargumentnullexception

asp.net mvc 4.6 dropdownlist give "System.ArgumentNullException: 'Value cannot be null. Parameter name: items' " Error


I tried MVC model to have a Store Procedure, Class, API controller, CreateController and Create View.

For some reason, when I try to use the Dropdownlist, it have error and point to the View

System.ArgumentNullException: 'Value cannot be null.

Parameter name: items'

<div class="form-group">
    @Html.LabelFor(model => model.LH, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
       @Html.DropDownListFor(r => r.LH, new SelectList(ViewBag.Type, "Value", "Text"), new { @class = 
       "form-control" })
       @Html.ValidationMessageFor(model => model.LH, "", new { @class = "text-danger" })
    </div>
</div>

Above is code in my View

private ELSEntities db = new ELSEntities();

        public void Dropdownlist()
        {
            //Populate my List
            IList<SelectListItem> ActivityType = new List<SelectListItem>();
            ActivityType.Add(new SelectListItem { Text = "", Value = ""});
            ActivityType.Add(new SelectListItem { Text = "Video", Value = "Video" });
            ActivityType.Add(new SelectListItem { Text = "Test", Value = "Test" });
            //-----Convertion SelectListItem to SelectList          
            SelectList actT = new SelectList(ActivityType, "Value", "Text","0");
            ViewBag.Type = actT;
        }

        //Create controllers for the view
        public ActionResult Create()
        {
            Dropdownlist();
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind] MultipleActivityClass mta)
        {
            if (ModelState.IsValid)
            {
                HttpClient hc = new HttpClient();
                hc.BaseAddress = new Uri("http://localhost:53132/api/MultiAct");


                var c = hc.PostAsJsonAsync<MultipleActivityClass>("MultiAct", mta);
                c.Wait();
            }
            return View(mta);
        }

That's my Controller for create

        public IHttpActionResult MultiActCreate(MultipleActivityClass mt)
        {
            ELSEntities db = new ELSEntities();
            //The store procedure name: MultipleActivity
            db.MultipleActivity(mt.LH);
            db.SaveChanges();
            return Ok();
        }

And here is the API controller that I used in the Controller, which use the class MultipleActivityClass to insert according to a Stored Procedure

    public class MultipleActivityClass
    {
        public string LH { get; set; }
    }

So is there anyway to fix this problem? I tried to search for same problem but there are no good solution for this.


Solution

  • And so the easiest way to solve this problem is to not use API,

    which mean change Create controller from

            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind] MultipleActivityClass mta)
            {
                if (ModelState.IsValid)
                {
                    HttpClient hc = new HttpClient();
                    hc.BaseAddress = new Uri("http://localhost:53132/api/MultiAct");
    
    
                    var c = hc.PostAsJsonAsync<MultipleActivityClass>("MultiAct", mta);
                    c.Wait();
                }
                return View(mta);
            }
    

    into

            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind] MultipleActivityClass mta)
            {
                if (ModelState.IsValid)
                {
                    db.MultipleActivity(mta.LH);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(mta);
            }
    

    I do not know why, if anyone can explain it, please do, thank you.