public JsonResult Create([Bind(Include = "dateAt,dateType,remark")] WorkAndRest workAndRest) { ResultMessage msg = new ResultMessage(); try { msg = DoValidation(workAndRest); if (!msg.Success) { return(Json(msg, JsonRequestBehavior.AllowGet)); } else { IWorkAndRestService wrs = new WorkAndRestService(Settings.Default.db); if (!wrs.HasDateAtExist(workAndRest.dateAt)) { msg.Success = false; msg.Content = "日期已存在,不可重复添加, 请进行编辑"; } bool isSucceed = wrs.Create(workAndRest); msg.Success = isSucceed; msg.Content = isSucceed ? "" : "添加失败"; return(Json(msg, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { return(Json(new ResultMessage() { Success = false, Content = ex.Message }, JsonRequestBehavior.AllowGet)); } }
/// <summary> /// 验证 /// </summary> /// <param name="model"></param> /// <param name="dbString"></param> public void Validate(string dbString) { ValidateMessage msg = new ValidateMessage(); if (string.IsNullOrEmpty(this.DateAtStr)) { msg.Contents.Add("日期不可空"); } if (!DateAt.HasValue) { msg.Contents.Add("日期格式不正确"); } IWorkAndRestService wrs = new WorkAndRestService(dbString); bool HasExist = wrs.HasDateAtExist(DateAt); if (!HasExist) { msg.Contents.Add("日期已经存在, 请进行编辑"); } if (string.IsNullOrEmpty(this.DateTypeStr)) { msg.Contents.Add("类型不可空"); } if (!DateType.HasValue) { msg.Contents.Add("类型不正确,只能为(法定假日/调休日/节假日)、(公休日/双休日)、(工作日)"); } msg.Success = msg.Contents.Count == 0; this.ValidateMessage = msg; }