DropdownList值在ASP.NET MVC中为空

时间:2019-01-20 17:52:16

标签: c# asp.net-mvc

操作:

[HttpPost]
public ActionResult Index(Account account, FormCollection fc)
{   
    using (accountgoEntities entities = new accountgoEntities())
    {
        entities.Account.Add(account);
        entities.SaveChanges();
        int id = account.Id;
    }

    //return View(Account);
    return  RedirectToAction("Account_details");
}

查看:

<div class="form-group">
    <strong class="strong">company name  </strong>
    <div class="col-md-10">
        @Html.DropDownList("CompanyName", (IEnumerable<SelectListItem>)ViewBag.CompanyName, "chose", new { @class = "form-control" })

        @Html.ValidationMessageFor(model => model.Company.Name, "", new { @class = "text-danger" })
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

如您在评论中所说:

  

提交表格companyId = null

这实际上是因为您的下拉选择输入字段名称是CompanyName而不是CompanyId

因此,将其命名为CompanyId,如下所示:

<div class="form-group">
    <strong class="strong">Company Name</strong>
    <div class="col-md-10">
        @Html.DropDownList("CompanyId", (IEnumerable<SelectListItem>)ViewBag.CompanyName, "Select Company", new { @class = "form-control" })

        @Html.ValidationMessageFor(model => model.CompanyId, "", new { @class = "text-danger" })
    </div>
</div>

现在CompanyId的帖子将具有一个值。