ASP.NET MVC-提交表单下拉列表错误错误



我是ASP.NET MVC的新手,并试图学习如何使用验证提交表单。我的创建视图正常工作,它列出了Platdownlists的字段,但是当我提交时,我收到了此错误System.ArgumentNullException: Value cannot be null.。我已经阅读了几个小时,无法正常工作。需要帮助...

这是我的公司班:

[Table("tblCompany")]
public class Company
{
    public int CompanyID { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    [DisplayName("Profile")]
    public string Type2 { get; set; }
    public string Industry { get; set; }
}

homecontroller中的动作:

public ActionResult CreateCompany()
        {
            // Assume these two lists are working fine.  The form Dropdownlist do list the options form these lists.
            ViewBag.CompanyType = GenericMethods.GetGenericPicks2("CompanyType2").OrderBy(e => e.Name);
            ViewBag.CompanyIndustry = GenericMethods.GetGenericPicks2("CompanyIndustry").OrderBy(e => e.Name);
            return View();
        }
[HttpPost]
public ActionResult CreateCompany(Company comp)
    {
        if (ModelState.IsValid)
        {
            Response.Write("Saving to DB");
            // code to save to database, redirect to other page
            return View(comp);
        }
        else
        {
            return View(comp);
        }

    }

最后,这是创建视图。此视图正常工作,显示带有选项的下拉列表...,但是当我单击提交时,我收到了上述错误。

@model MVCTest1.Models.Company
@{
    ViewBag.Title = "CreateCompany";
}
<h2>CreateCompany</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <div class="form-horizontal">
        <h4>Company</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Type2, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Type2, new SelectList(ViewBag.CompanyType, "Value", "Name"), "Select Option")
                @Html.ValidationMessageFor(model => model.Type2, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Industry, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.Industry, new SelectList(ViewBag.CompanyIndustry, "Value", "Name"), "Select Option")
                @Html.ValidationMessageFor(model => model.Industry, "", 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>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

您的帮助将不胜感激。

@stephenmuecke已经提出了更改,您还需要在Post方法中填充ViewBag,以便您可以使用视图。

[HttpPost]
public ActionResult CreateCompany(Company comp)
    {
        ViewBag.CompanyType = GenericMethods.GetGenericPicks2("CompanyType2").OrderBy(e => e.Name);
        ViewBag.CompanyIndustry = GenericMethods.GetGenericPicks2("CompanyIndustry").OrderBy(e => e.Name);
        if (ModelState.IsValid)
        {
            Response.Write("Saving to DB");
            // code to save to database, redirect to other page
            return View(comp);
        }
        else
        {
            return View(comp);
        }
    }

最新更新