检查对象属性是否有空值

 public ActionResult Create(crm_CompetitiveProducts cp)
        {
            using (var db = new BaseDbContext())
            {
                PropertyInfo[] propertys = cp.GetType().GetProperties();
                int a = 0;
                foreach (var i in propertys)
                {
                    if (i.GetValue(cp) == null)
                    {
                        a = a + 1;
                    }
                }
                if (a == 0)
                {
                    db.Insert<crm_CompetitiveProducts>(cp);
                }
                else
                {
                    this.ShowMessage("信息不全", "录入失败");
                    return RedirectToAction("Create");
                }
                return RedirectToAction("Index");
            }
        }

 

posted @ 2015-09-14 10:54  九省巡按  阅读(352)  评论(0编辑  收藏  举报