C# ArgumentNullException:值不能为null。参数名称:items selectlist.net core

C# ArgumentNullException:值不能为null。参数名称:items selectlist.net core,c#,asp.net-core,C#,Asp.net Core,Selectlist对于Get请求非常有效。但是对于Post请求,它会给出null异常 @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" }) 控制器代码: var categ

Selectlist对于
Get
请求非常有效。但是对于
Post
请求,它会给出
null
异常

@Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
控制器代码:

var category = _context.CategoryTbl.ToList();
List<SelectListItem> li = new List<SelectListItem>();
li.Add(new SelectListItem { Text = "Select Category", Value = "0" });
foreach (var m in category)
{
     li.Add(new SelectListItem { Text = m.Name, Value = m.Id.ToString() });
     ViewBag.category =li;
}
@Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
我发现以下错误:

@Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
ArgumentNullException:值不能为null。参数名称:items

@Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
三件事:

@Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
  • 将选项存储为视图模型上的属性更合适。这就解决了类似于动力学的问题。当您保持强类型时,追踪问题会容易得多

  • @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
  • 您不需要创建
    选择列表
    。所需要的只是一个您已经拥有的
    IEnumerable

  • @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
  • 最好在此处使用
    SelectTagHelper

  • @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    综上所述,在您的模型上添加:

    @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    public IEnumerable<SelectListItem> CategoryOptions { get; set; }
    
    public IEnumerable CategoryOptions{get;set;}
    
    那么在你看来,

    @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    <select asp-for="Category" asp-items="@Model.CategoryOptions"></select>
    
    
    
    三件事:

    @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
  • 将选项存储为视图模型上的属性更合适。这就解决了类似于动力学的问题。当您保持强类型时,追踪问题会容易得多

  • @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
  • 您不需要创建
    选择列表
    。所需要的只是一个您已经拥有的
    IEnumerable

  • @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
  • 最好在此处使用
    SelectTagHelper

  • @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    综上所述,在您的模型上添加:

    @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    public IEnumerable<SelectListItem> CategoryOptions { get; set; }
    
    public IEnumerable CategoryOptions{get;set;}
    
    那么在你看来,

    @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    <select asp-for="Category" asp-items="@Model.CategoryOptions"></select>
    

    它与您的
    POST
    操作相关,如果您返回相同的视图,您还需要一个
    ViewBag.category

    @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    您可以使用get方法中的方式在返回视图之前对其进行初始化,也可以简单地使用
    newselectlist(\u context.CategoryTbl,“Id”,“Name”)
    ,请参阅以下代码:

    @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    [HttpPost]  
    public async Task<IActionResult> MyPostMethodName(Model model)
        {
            if (!ModelState.IsValid)
            {
    
            }
            //your post logic
            //...
            //need a  ViewBag.category if you return the same view,
            ViewBag.category = new SelectList(_context.CategoryTbl, "Id", "Name", model.Category);
    
            return View(product);
        }
    
    [HttpPost]
    公共异步任务MyPostMethodName(模型)
    {
    如果(!ModelState.IsValid)
    {
    }
    //你的后逻辑
    //...
    //如果返回相同的视图,则需要ViewBag.category,
    ViewBag.category=new SelectList(_context.CategoryTbl,“Id”,“Name”,model.category);
    返回视图(产品);
    }
    
    它与您的
    POST
    操作相关,如果您返回相同的视图,您还需要一个
    ViewBag.category

    @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    您可以使用get方法中的方式在返回视图之前对其进行初始化,也可以简单地使用
    newselectlist(\u context.CategoryTbl,“Id”,“Name”)
    ,请参阅以下代码:

    @Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.category, "Value", "Text"), new { @id = "ddlCategory", style = "width: 200px;", @class = "form-control input-lg" })
    
    [HttpPost]  
    public async Task<IActionResult> MyPostMethodName(Model model)
        {
            if (!ModelState.IsValid)
            {
    
            }
            //your post logic
            //...
            //need a  ViewBag.category if you return the same view,
            ViewBag.category = new SelectList(_context.CategoryTbl, "Id", "Name", model.Category);
    
            return View(product);
        }
    
    [HttpPost]
    公共异步任务MyPostMethodName(模型)
    {
    如果(!ModelState.IsValid)
    {
    }
    //你的后逻辑
    //...
    //如果返回相同的视图,则需要ViewBag.category,
    ViewBag.category=new SelectList(_context.CategoryTbl,“Id”,“Name”,model.category);
    返回视图(产品);
    }
    
    您可以发布整个异常堆栈吗?不仅仅是错误消息。你能发布整个异常堆栈吗?不仅仅是错误信息。