ASP.NET MVC - 提交表单DropDownListFor Error

时间:2018-04-25 22:09:14

标签: c# asp.net asp.net-mvc

我是ASP.NET MVC的新手,并尝试学习如何通过验证提交表单。我的“创建”视图工作正常,它使用下拉列表列出字段,但是当我提交时,我收到此错误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>

我们将不胜感激。

1 个答案:

答案 0 :(得分:2)

由于@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);
        }
    }