Ошибка на строительных лесах MVC: "Значение не может быть нулевым. Имя параметра: источник"

Я следовал инструкции в этом посте post, но когда я пытаюсь добавить продукт, я получаю эту ошибку:

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: 
Line 64:
Line 65: @Html.DropDownListFor(model => model.CategoryId, ((IEnumerable)ViewBag.PossibleCategories).Select(option => new SelectListItem { Line 66: Text = (option == null ? "None" : option.Name), Line 67: Value = option.Id.ToString(),

Код контроллера:

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);
}

И код вида:

 @Html.DropDownListFor(model => model.CategoryId, ((IEnumerable)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)

5
задан Zoe 5 June 2019 в 18:52
поделиться