在我的控制器中,这段代码:
[HttpPost]
public ActionResult Edit(Company company, FormCollection IsCostCenters)
{
if (ModelState.IsValid)
{
Company objNewCompany = new Company();
//oParty.CostCenters.Clear();
using (PaymentAdviceEntityContainer db1 = new PaymentAdviceEntityContainer())
{
objNewCompany = db1.Companies.Find(company.Id);
objNewCompany.CostCenters.Clear();
string[] temp = IsCostCenters["CostCenters"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var s in temp)
{
if (s != "false")
{
CostCenter oCostCenter = new CostCenter();
oCostCenter = db1.CostCenters.Find(Convert.ToInt32(s));
objNewCompany.CostCenters.Add(oCostCenter);
}
}
db1.SaveChanges();
}
db.Entry(company).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CreatedById = new SelectList(db.Employees, "Id", "FirstName", company.CreatedById);
return View(company);
}
我的视图代码如下
@using PaymentAdviceEntity;
@{
ViewBag.Title = "Edit";
List<CostCenter> clist = new List<CostCenter>();
clist = ((List<CostCenter>)ViewBag.CostCenters).ToList();
}
<div style="line-height: 22px; width: 100%;height :3%; float: left; ">
@{
foreach (var item in clist)
{
<div style="line-height: 22px; width: 28%; float: left;">
<span class="checkbox">@Html.CheckBox("CostCenters", item.IsChecked, new { @value = item.Id })</span>
<span>@Html.DisplayFor(modelItem => item.Name)</span>
</div>
}
}
ModelState.IsValid
那么请在页面发布时间返回false的原因是什么......