Asp.net mvc MVC脚手架错误:“;值不能为null。参数名称:source";
我按照本文中的说明进行操作,但当我尝试添加产品时,出现以下错误:Asp.net mvc MVC脚手架错误:“;值不能为null。参数名称:source";,asp.net-mvc,asp.net-mvc-3,html.dropdownlistfor,scaffolding,Asp.net Mvc,Asp.net Mvc 3,Html.dropdownlistfor,Scaffolding,我按照本文中的说明进行操作,但当我尝试添加产品时,出现以下错误: Server Error in '/' Application. -------------------------------------------------------------------------------- Value cannot be null. Parameter name: source Description: An unhandled exception occurred during the
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(),
该视图的代码为:
@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)
@Html.DropDownListFor(model=>model.CategoryId,((IEnumerable)ViewBag.PossibleCategories)。选择(option=>newselectListItem){
Text=(option==null?“无”:option.Name),
Value=option.Id.ToString(),
所选=(Model!=null)&&(option.Id==Model.CategoryId)
}),“选择…”)
@Html.ValidationMessageFor(model=>model.CategoryId)
您的问题如下:
您可以在控制器中分配此属性:
ViewBag.PossibleCategory = context.Categories;
然后,在视图中
尝试读取此动态视图包
属性:
ViewBag.PossibleCategories
你能看到错误吗?你给了不同的名字。。。由于ViewBag
使用了新的C#4,因此无法进行编译时检查<代码>视图包。可能的类别仅在运行时解析。由于没有与ViewBag
属性匹配的ViewBag。可能的类别
会出现以下错误:值不能为null。参数名称:源
要解决此问题,请执行以下操作:
ViewBag.PossibleCategories = context.Categories;
我在.NET5(.netcore)中收到了这个错误,带有EFCore5和MVC
Value cannot be null. Parameter name: connectionString
问题是我没有appsettings.json文件,因此没有数据库的连接字符串,因此脚手架无法连接到数据库。非常好的捕获!!我也遇到了同样的问题,我一辈子都看不出哪里出了问题。thanx,这个陷阱救了我一天:)亲爱的@gatam:如果答案解决了你的问题,你应该接受它