Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ArgumentNullException:值不能为null。参数名称:items_C#_Mysql_Asp.net Mvc - Fatal编程技术网

C# ArgumentNullException:值不能为null。参数名称:items

C# ArgumentNullException:值不能为null。参数名称:items,c#,mysql,asp.net-mvc,C#,Mysql,Asp.net Mvc,我已经看到了这个问题的多次迭代,涉及DropDownList for tag helper多于select tag helper我的问题,例如和,但是我还没有找到适合我的答案,所以我正在寻求帮助 stacktrace中突出显示的代码行来自我的文件AlertSelect.cshtml: asp-items="@(new SelectList(@ViewBag.ListofIdentifier, "AlertIndex", "Alert_Identifier"))"></select&g

我已经看到了这个问题的多次迭代,涉及DropDownList for tag helper多于select tag helper我的问题,例如和,但是我还没有找到适合我的答案,所以我正在寻求帮助

stacktrace中突出显示的代码行来自我的文件AlertSelect.cshtml:

asp-items="@(new SelectList(@ViewBag.ListofIdentifier, "AlertIndex", "Alert_Identifier"))"></select>
“items”引用MySql数据库中EdxlCapMsg表的AlertIndex和Alert_Identifier列,该表预先填充了3条记录。我真的很困惑,因为我在Visual Studio 2017的一个单独的解决方案中使用另一个MySql数据库中的相同表从中创建了这个dropdownlist,到目前为止,它工作正常。你可以看到我在哪里注释掉了代码,这些代码将适用于使用dropdownlist中的选择来选择要在更大的应用程序中编辑的记录

由于该表已填充,因此在显示之前,我看不到该值如何为null

提前谢谢。我肯定这是个愚蠢的错误,但我没有看到

这是控制器:

public class EdxlCapMsgController : Controller
{
    private readonly ApplicationDbContext _context;

    public EdxlCapMsgController(ApplicationDbContext context)
    {
        _context = context;
    }

    // GET: EdxlCapMessages
    public IActionResult Index()
    {
        List<EdxlCapMsg> identifierlist = new List<EdxlCapMsg>();

        //------Getting Data fom Database using EntityFrameworkCore------
        identifierlist = (from product in _context.EdxlCapMsg
                          select product).ToList();

        //------Inserting Select Item in List------
        identifierlist.Insert(0, new EdxlCapMsg { AlertIndex = 0, Alert_Identifier = "Select" });

        //------Assigning identifierlist to ViewBag.ListofIdentifier------
        ViewBag.ListofIdentifier = identifierlist;


        return View();
    }

    [HttpPost]

    public IActionResult Index(EdxlCapMsg EdxlCapMsg)
    {
        //------Validation------
        if (EdxlCapMsg.AlertIndex == 0)
        {
            ModelState.AddModelError("", "Select EdxlCapMsg");
        }

        //------Getting selected value------
        int SelectedValue = EdxlCapMsg.AlertIndex;

        ViewBag.SelectedValue = EdxlCapMsg.AlertIndex;

        //------Setting Data back to ViewBag after Posting form------
        List<EdxlCapMsg> identifierlist = new List<Models.EdxlCapMsg>();

        identifierlist = (from product in _context.EdxlCapMsg
                          select product).ToList();

        identifierlist.Insert(0, new EdxlCapMsg { AlertIndex = 0, Alert_Identifier = "Select" });
        ViewBag.ListofIdentifier = identifierlist;

        return View();
    }

}
}

以下是ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
        this.Database.EnsureCreated();
    }

    public DbSet<EdxlCapMsg> EdxlCapMsg { get; set; }

    public virtual DbSet<Person> Persons { get; set; }

    public DbSet<Alert> Alert { get; set; }

    public DbSet<Area> Area { get; set; }

    public DbSet<DataCategory> DataCategory { get; set; }

    public DbSet<Element> Element { get; set; }

    public DbSet<Info> Info { get; set; }

    public DbSet<Resource> Resource { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
....

对于SelectList,EdxlCapMsg是主要的重要类。

在控制器的响应进入razor引擎之前,您是否调试过它,以确保您确实正确填充了数据?不,我还没有这样做。谢谢你。不,我没有那样做。谢谢我不太确定如何调试来自控制器的响应。我将添加此SelectList和ApplicationDBContext的控制器代码,并询问如何确保正确填充数据。我为SelectList单独填充了表格。您的代码看起来不错。你什么时候收到这个异常?这是第一个GET请求还是在您提交表单之后?我在“asp items=@new”行中得到了一个异常SelectList@ViewBag.ListofIdentifier,AlertIndex,Alert_Identifier>,在查看页面AlertSelect cshtml中,如上所示。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
        this.Database.EnsureCreated();
    }

    public DbSet<EdxlCapMsg> EdxlCapMsg { get; set; }

    public virtual DbSet<Person> Persons { get; set; }

    public DbSet<Alert> Alert { get; set; }

    public DbSet<Area> Area { get; set; }

    public DbSet<DataCategory> DataCategory { get; set; }

    public DbSet<Element> Element { get; set; }

    public DbSet<Info> Info { get; set; }

    public DbSet<Resource> Resource { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
....