1

I know this question has been asked already here in SO, but I can't distinguish my error from this post : DropDownListFor Not Selecting Value

I did not use ViewBag's name the same as the model's property coming from the link.

Here is my code in the controller:

public ActionResult Create()
    {
        ViewBag.Companies = db.Companies.ToList();
        return View();
    }

and this is my code in the view:

<div class="editor-field">
        @Html.DropDownListFor(model => model.ParentId, new SelectList(ViewBag.Companies, "Id", "ShortName"), "Select a Category")
        @Html.ValidationMessageFor(model => model.ParentId)
    </div>

in my create form, the dropdown list generates without error, however after clicking create, an error states "value cannot be null" even though I have selected a value.

I even added the ViewBag again in the [POST]Create Action :

 [HttpPost]
    public ActionResult Create(Company company)
    {
        ViewBag.Companies = db.Companies.ToList();
        if (ModelState.IsValid)
        {
            db.Companies.AddObject(company);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(company);
    }

I don't know where the error resides, I am completely new to ASP.NET MVC.

Here is the error log:

enter image description here

Community
  • 1
  • 1
Break the Law
  • 217
  • 2
  • 14
  • Do you mean when you return the view after the post? (if so, you need to reassign `ViewBag.Companies` before returning the view) –  Feb 23 '15 at 03:57
  • When saving the newly created data from the form. What do you mean need to reassign? Sorry for noobish question. – Break the Law Feb 23 '15 at 04:01
  • You edit shows that you reassign the `ViewBag` property so that's OK (although you should move it to the line immediately before `return View(company);` - no point doing it if you redirect so its unnecessary extra overhead). Can you post the full details of the error. –  Feb 23 '15 at 04:08
  • 2
    That error is saying that the first parameter of the `SelectList()` constructor (`IEnumerable items`) is `null`, meaning that `ViewBag.Companies` is null when you return the view. Are you sure your code is exactly as you have posted? –  Feb 23 '15 at 04:24
  • Put a breakpoint in the controller and in the view, and inspect ViewBag.Companies – Yishai Galatzer Feb 23 '15 at 04:25
  • Unrelated: If this is new code you should use MVC 5.2.X (preferrably 5.2.3) – Yishai Galatzer Feb 23 '15 at 04:25
  • @StephenMuecke I found the error : "Select a Category" <- this is causing the error. However, I want that to be the default value if none is selected. – Break the Law Feb 23 '15 at 04:35
  • @BreaktheLaw, That has nothing to do with it. If the value of `ParentId` does not match one of the option values (defined by the `Id` property of `Company` then the first ("Select a Category") option will be selected. The stack trace clearly shows that the value of `ViewBag.Companies` is `null` –  Feb 23 '15 at 04:38
  • @StephenMuecke Yes I am sure. I posted the needed codes in detail for my issue to be fully understood. But why is the error not displaying anymore when I removed the Select a Category? – Break the Law Feb 23 '15 at 04:49
  • Changing `@Html.DropDownListFor(model => model.ParentId, new SelectList(ViewBag.Companies, "Id", "ShortName"), "Select a Category")` to `@Html.DropDownListFor(model => model.ParentId, new SelectList(ViewBag.Companies, "Id", "ShortName"))` will make no difference unless you have some other problems with you code. All the 3rd parameter does is perpend `` to the ` –  Feb 23 '15 at 04:56
  • Suddenly I reverted back the default option Select a Category in my view., and it still works, I look like an idiot here, wondering how did that work. I did not change any code in my controller – Break the Law Feb 23 '15 at 04:59

0 Answers0