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

时间:2018-06-13 19:52:07

标签: c# mysql asp.net-mvc

我已经看过很多这个问题的迭代,涉及dropdownlistfor标签助手比select herehere选择标签助手(我的问题)更多,但是我没有找到有效的答案对我来说,所以我正在寻求帮助。

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

asp-items="@(new SelectList(@ViewBag.ListofIdentifier, "AlertIndex", "Alert_Identifier"))"></select>
@model edxl_cap_v1_2.Models.EdxlCapMsg

@{
    <form asp-controller="EdxlCapMsg" asp-action="Index" method="post" 
class="form-horizontal" role="form">
        <div class="form-group">
            <div class="alert-danger" asp-validation-summary="ModelOnly"></div>
            <div class="content-wrapper">
                <span class="smallText">
                    <label asp-for="Alert_Identifier" class="control-label">   </label>
                    <select asp-for="AlertIndex"
                            class="form-control"
                            asp-items="@(new SelectList(@ViewBag.ListofIdentifier, "AlertIndex", "Alert_Identifier"))"></select>
                </span>
            </div>
        </div>
        @*<div class="form-group">
            <div class="content-wrapper">
                <input id="Submit1" type="submit" value="submit" />
            </div>
        </div>
        <div class="form-group">
            <div class="content-wrapper">
                @if (ViewBag.SelectedValue != null)
                {
                    <text>Selected Alert_Identifier: </text> @ViewBag.SelectedValue;
                }
            </div>
        </div>*@
    </form>
}

'items'引用MySql数据库中EdxlCapMsg表的AlertIndex和Alert_Identifier列,该列预先填充了3条记录。我真的很难过,因为我在Visual Studio 2017中的一个单独的解决方案中使用tutorial创建了这个下拉列表,并在另一个MySql数据库中使用相同的表,并且它可以正常工作。 (你可以看到我在哪里注释了将适用于使用下拉列表中的选项来选择要在更大的应用程序中进行编辑的记录的代码。)

由于表是填充的,因此我甚至没有看到该值在显示之前如何为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是主要的重要类。

0 个答案:

没有答案