MVC Scaffolding error: "Value cannot be null. Parameter name: source"
Asked Answered
C

2

6

I followed the instruction in this post, but when I try to add a product I get this error:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Value cannot be null.
Parameter name: source 
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: source

Source Error: 


Line 63: </div>
Line 64: <div class="editor-field">
Line 65:     @Html.DropDownListFor(model => model.CategoryId, ((IEnumerable<GAM.Models.Category>)ViewBag.PossibleCategories).Select(option => new SelectListItem {
Line 66:         Text = (option == null ? "None" : option.Name), 
Line 67:         Value = option.Id.ToString(),

The controller code is:

public ActionResult Create()
{
    ViewBag.PossibleCategory = context.Categories;
    return View();
} 

//
// POST: /Product/Create

[HttpPost]
public ActionResult Create(Product product)
{
    if (ModelState.IsValid)
    {
        context.Products.Add(product);
        context.SaveChanges();
        return RedirectToAction("Index");  
    }

    ViewBag.PossibleCategory = context.Categories;
    return View(product);
}

And the code of the view is:

 @Html.DropDownListFor(model => model.CategoryId, ((IEnumerable<GAM.Models.Category>)ViewBag.PossibleCategories).Select(option => new SelectListItem {
    Text = (option == null ? "None" : option.Name), 
    Value = option.Id.ToString(),
    Selected = (Model != null) && (option.Id == Model.CategoryId)
}), "Choose...")
@Html.ValidationMessageFor(model => model.CategoryId)
Cormac answered 6/6, 2011 at 3:20 Comment(0)
D
11

Your problem is the following:

You assign this property within the Controller:

ViewBag.PossibleCategory = context.Categories;

Then, in your View you try to read this dynamic ViewBag property:

ViewBag.PossibleCategories

Can you see the error? You're giving different names... You do not get compile time checking because ViewBag uses the new C# 4 dynamic type. ViewBag.PossibleCategories will only be resolved at runtime. As there's no ViewBag property that matches ViewBag.PossibleCategories you get this error: Value cannot be null. Parameter name: source

To solve this just do this:

ViewBag.PossibleCategories = context.Categories;
Dote answered 6/6, 2011 at 3:34 Comment(1)
Very nice catch!! I had the exact same problem and couldn't for my life see what was wrong.Benjie
A
0

I received this error in .NET 5 (.NET Core), with EF Core 5 and MVC.

Value cannot be null. Parameter name: connectionString

The problem was that I didn't have an appsettings.json file, and therefore didn't have the connectionstrings for the database, so the scaffolding could not connect to the database.

Allocution answered 4/2, 2021 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.