gpt4 book ai didi

asp.net-mvc - MVC 脚手架错误 : "Value cannot be null. Parameter name: source"

转载 作者:行者123 更新时间:2023-12-02 18:17:18 26 4
gpt4 key购买 nike

我按照此 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: </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(),

Controller 代码是:

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

View 的代码是:

 @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)

最佳答案

您的问题如下:

您可以在 Controller 中分配此属性:

ViewBag.PossibleCategory = context.Categories;

然后,在您的 View 中尝试读取此动态 ViewBag 属性:

ViewBag.PossibleCategories

你能看到错误吗?您给出了不同的名称...您无法进行编译时检查,因为 ViewBag 使用新的 C# 4 dynamic typeViewBag.PossibleCategories 只会在运行时解析。由于没有与 ViewBag.PossibleCategories 匹配的 ViewBag 属性,您会收到此错误:Value 不能为 null。参数名称:source

要解决这个问题,只需这样做:

ViewBag.PossibleCategories = context.Categories;

关于asp.net-mvc - MVC 脚手架错误 : "Value cannot be null. Parameter name: source",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6247500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com