获取已填写的表单字段的数量?

时间:2019-03-19 19:45:13

标签: c# asp.net-mvc

控制器:

    [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对象获得一个计数,该计数是上一页中表单上填写的字段数。例如,如果文本框保留为空或未选中复选框,则不包括在计数中。这可以设置吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

int count = accidentSupervisor.GetType()
                 .GetProperties()
                 .Select(s => s.GetValue(accidentSupervisor, null))
                 .Count(c => c != null);