[英]DropdownList value is getting null in ASP.NET MVC
Action:行动:
[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");
}
View:看法:
<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>
As you said in the comment that:正如您在评论中所说:
after submitting the form companyId = null提交表格 companyId = null 后
This is actually becuase your drop down select input field name is CompanyName
instead of CompanyId
这实际上是因为您的下拉选择输入字段名称是CompanyName
而不是CompanyId
So name it CompanyId
as follows:因此,将其命名为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>
Now CompanyId
will have a value on post.现在CompanyId
将在 post 上有一个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.