C# razorview DropDownListFor 'Value cannot be null'

C# razorview DropDownListFor 'Value cannot be null'

我是 ASP.NET MVC 的新手,我正在开发我的第一个项目只是为了好玩。 我得到这个 ArgumentNullException,但我不知道出了什么问题。

这是我的模型:

public class SpeciesLabel
{
    [Key]
    [Required]
    public string Name { get; set; }

    [Required]
    public CustomGroup CustomGroup { get; set; }

    [Required]
    public Family Family { get; set; }

    [Required]
    public Genus Genus { get; set; }

    [Required]
    public Species Species { get; set; }
}

public class SpeciesLabelDbContext : DbContext
{
    public SpeciesLabelDbContext()
        : base("DefaultConnection")
    {

    }

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

这是控制器:

 public ActionResult Create()
    {
        List<SelectListItem> customGroups = new List<SelectListItem>();
        IQueryable<string> customGroupsQuery = from g in customGroupsDb.CustomGroup
            select g.Name;

        foreach (var element in customGroupsQuery)
        {
            customGroups.Add(new SelectListItem()
            {
                Value = element,
                Text = element
            });
        }

        ViewBag.CustomGroup = customGroups;

这是控制器POST请求:

public ActionResult Create([Bind(Include = "CustomGroup,Family,Genus,Species")] SpeciesLabel speciesLabel)
    {
        if (ModelState.IsValid)
        {
            db.SpeciesLabel.Add(speciesLabel);
            db.SaveChanges();
            return RedirectToAction("Create");
        }

        return View();
    }

这是视图:

    <pre>
        @model PlantM.Models.PlantModels.SpeciesLabel

@{
    ViewBag.Title = "Create";
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Species label</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.CustomGroup, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.CustomGroup, new SelectList(ViewBag.CustomGroupList, "Value", "Text"), "Please select...", new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CustomGroup, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
</pre>

我有视图中所有属性的输入,但我删除了它们,因为它们与这个相似,例外情况也相同。只有 属性 名称不会从视图返回,因为它将在控制器中设计(其他属性的串联)。

这是我提交表单时遇到的异常:

ArgumentNullException

编辑:

在 POST Create 方法中添加 ViewBag 初始化后,ArgumentNullException 的问题得到解决,但我仍然收到 Null 值参数,因此无法创建对象,Create 视图再次被调用并且再次!?谁能告诉我为什么这些@Html.DropDownListFor 对控制器 post 没有任何价值?

从评论来看,听起来好像您在第一次访问时看到了视图,但是在您 post.

之后发生了 null 异常

如果以上假设是正确的,那么我认为你的问题是因为当你post返回时,你的模型没有通过验证(例如,可能是required input field did not post back value), 这意味着 ModelState.IsValid 是假的,所以 return View() 被称为

问题在这里,你没有设置 return 之前的 ViewBag.CustomGroup = customGroups;,因此 ViewBag.CustomGroup 为空,这就是为什么你看到了异常。

像您在 get 上那样初始化 ViewBag 然后您应该能够看到该页面。