繁体   English   中英

C#razorview DropDownListFor'值不能为空'

[英]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不向控制器发布任何值?

从评论看来,您第一次访问时就可以看到视图,但是发布后会发生空异常。

如果上述假设正确,那么我认为您的问题是因为回发时, 您的模型未通过验证 (例如,可能必填的输入字段未回发值),这意味着ModelState.IsValid为false,因此return View()被调用

这是问题所在, 您未设置 ViewBag.CustomGroup = customGroups; 在返回之前,因此ViewBag.CustomGroup为null,这就是您看到异常的原因。

初始化ViewBag,就像您在获取时一样,那么您应该可以看到页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM