控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Facility,DateCreated,CreateUsername,DateLastModified,LastModifiedUsername,SupervisorComplete,DeptManagerComplete,SafetyManagerComplete,SupervisorDateCompleted,DeptManagerDateCompleted,SafetyManagerDateCompleted,Deleted,CorrectiveActionComplete,CorrectiveActionDateCompleted,HospitalName,EmailSent")] AccidentSupervisor accidentSupervisor)
{
if (ModelState.IsValid)
{
//Add AccidentSupervisor
DB.AccidentSupervisors.Add(accidentSupervisor);
DB.SaveChanges();
return RedirectToAction("Index");
}
return View(accidentSupervisor);
}
我基本上希望从accidentSupervisor对象获得一个计数,该计数是上一页中表单上填写的字段数。例如,如果文本框保留为空或未选中复选框,则不包括在计数中。这可以设置吗?
答案 0 :(得分:0)
尝试一下:
int count = accidentSupervisor.GetType()
.GetProperties()
.Select(s => s.GetValue(accidentSupervisor, null))
.Count(c => c != null);