c# - DropdownList 值在 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>
解决方案
正如你在评论中所说:
提交表单 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
将在帖子上具有价值。
推荐阅读
- python - Python 'e' 符号
- javascript - 在 onload 函数中访问 DOM 元素
- laravel - 获取MAC地址PC和手机有区别吗?
- sql - REGEX_LIKE 到 ~*
- javascript - 是否可以在 setInterval 中运行函数?
- regex - 只允许数字并且可以“包含”斜线并且不能以斜线开头或结尾
- javascript - 如何在函数中对条件进行单元测试?
- reactjs - 从具有 nginx 大小限制的 react 应用程序上传文件
- php - PHP正则表达式匹配
- docker-compose - FileBeat 未将 docker-container 日志发送到 Elastic-search