public ActionResult Index() { RecordModel model = new RecordModel(); string code = Request.QueryString["code"]; string state = Request.QueryString["state"]; if (!string.IsNullOrEmpty(code)) { model.OpenId = HttpHelper.GetOpenIdByAuthCode(code, state); } model.Distance = 0; model.RecordDate = DateTime.Now; ViewBag.Message = Resource.Resource.RecordDefault; return View(model); }
public ActionResult AddRecord(RecordModel model) { HttpPostedFileBase photo = Request.Files["file"]; this.Valid(model, photo); if (!ModelState.IsValid) { return View("Index", model); } else { string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads")); if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath); Debug.Assert(photo != null, "photo != null"); var fileName = Path.GetFileName(photo.FileName); Debug.Assert(fileName != null, "fileName != null"); photo.SaveAs(Path.Combine(filePath, fileName)); RecordDal db = new RecordDal(); tblRecord record = new tblRecord(); record.recorddate = model.RecordDate; record.distant = model.Distance; record.hour = model.Hour; record.minute = model.Minute; record.filepath = fileName; db.Insert(record); ViewBag.Message = Resource.Resource.RecordUploaded; } return View("Index"); }
public void Valid(RecordModel model, HttpPostedFileBase file) { if (file == null || file.ContentLength == 0) { ModelState.AddModelError("Id", Resource.Resource.RecordNoImage); } if (model.Distance <= 0) { ModelState.AddModelError("Distance", Resource.Resource.RecordNoDistant); } if (model.Hour <= 0 && model.Minute <=0) { ModelState.AddModelError("Hour", Resource.Resource.RecordNoTime); } }