public IHttpActionResult AddJobRequest() { JobRequestModel model = new JobRequestModel(); var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { model.ReferenceImages = new List <string>(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); IList <string> AllowedFileExtensions = new List <string> { ".jpg", ".png", ".jpeg", ".JPG", ".PNG", ".JPEG" }; var date = DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss").Replace("-", "_"); var count = httpRequest.Files.Count; if (count > 0) { for (int i = 0; i < httpRequest.Files.Count; i++) { if (httpRequest.Files[i] != null) { var postedImg = httpRequest.Files[i]; var ext = postedImg.FileName.Substring(postedImg.FileName.LastIndexOf('.')); var path = "~/Images/JobRequest/"; var imagePath = Path.GetFileNameWithoutExtension(postedImg.FileName) + "_" + date + ext.ToLower(); var fileName = path + imagePath; if (!AllowedFileExtensions.Contains(ext)) { return(this.Ok(new { status = false, message = Resource.file_extension_invalid })); } if (!Directory.Exists(HttpContext.Current.Server.MapPath(path))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path)); } postedImg.SaveAs(HttpContext.Current.Server.MapPath(fileName)); model.ReferenceImages.Add(imagePath); } } } } model.UserId = Convert.ToInt64(httpRequest.Form.Get("UserId")); if (httpRequest.Form.Get("CheckList") != null) { var checklist = httpRequest.Form.Get("CheckList").ToString(); model.CheckList = Newtonsoft.Json.JsonConvert.DeserializeObject <List <string> >(checklist); } model.PropertyId = Convert.ToInt64(httpRequest.Form.Get("PropertyId")); model.CategoryId = Convert.ToInt64(httpRequest.Form.Get("CategoryId")); if (httpRequest.Form.Get("JobStartDatetime") != null) { model.JobStartDatetime = Convert.ToDateTime(httpRequest.Form.Get("JobStartDatetime")); } if (httpRequest.Form.Get("JobEndDatetime") != null) { model.JobEndDatetime = Convert.ToDateTime(httpRequest.Form.Get("JobEndDatetime")); } if (httpRequest.Form.Get("Price") != null) { model.Price = Convert.ToDecimal(httpRequest.Form.Get("Price")); } if (httpRequest.Form.Get("ClientPrice") != null) { model.ClientPrice = Convert.ToDecimal(httpRequest.Form.Get("ClientPrice")); } if (httpRequest.Form.Get("HasPrice") != null) { model.HasPrice = Convert.ToBoolean(httpRequest.Form.Get("HasPrice")); } model.Description = httpRequest.Form.Get("Description"); model.SubCategoryId = Convert.ToInt64(httpRequest.Form.Get("SubCategoryId")); var subsubCat = httpRequest.Form.Get("SubSubCategories"); List <long> lst = new List <long>(); if (!string.IsNullOrEmpty(subsubCat)) { var subsubCatArr = subsubCat.Split(','); for (int i = 0; i < subsubCatArr.Length; i++) { var id = Convert.ToInt64(subsubCatArr[i]); lst.Add(id); } } model.SubSubCategories = lst; var status = propertyService.AddJobRequest(model); return(this.Ok(new { status = status, message = propertyService.message })); }
public JsonResult AddJobRequest(FormCollection formCollection) { JobRequestModel model = new JobRequestModel(); model.CategoryId = Convert.ToInt32(formCollection["CategoryId"]); model.PropertyId = Convert.ToInt32(formCollection["PropertyId"]); if (formCollection["JobStartDatetime"] != null && formCollection["JobStartTime"] != null) { var getDate = DateTime.ParseExact(formCollection["JobStartDatetime"], "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture); //var getDate = Convert.ToDateTime(formCollection["JobStartDatetime"]); TimeSpan time; if (TimeSpan.TryParse(formCollection["JobStartTime"], out time)) { string startDate = (getDate + time).ToString("yyyy-MM-dd HH:mm:ss"); model.JobStartDatetime = Convert.ToDateTime(startDate); } } if (formCollection["Price"] != "null" || formCollection["ClientPrice"] != "null") { model.HasPrice = true; } else { model.HasPrice = false; } if (formCollection["JobEndDatetime"] != null && formCollection["JobEndTime"] != null) { var getDate = DateTime.ParseExact(formCollection["JobEndDatetime"], "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture); //var getDate = Convert.ToDateTime(formCollection["JobEndDatetime"]); TimeSpan time; if (TimeSpan.TryParse(formCollection["JobEndTime"], out time)) { string endDate = (getDate + time).ToString("yyyy-MM-dd HH:mm:ss"); model.JobEndDatetime = Convert.ToDateTime(endDate); } } if (formCollection["CheckList"] != null) { var checklist = formCollection["CheckList"].ToString(); model.CheckList = Newtonsoft.Json.JsonConvert.DeserializeObject <List <string> >(checklist); } model.Description = formCollection["Description"]; model.UserId = Convert.ToInt64(Session["UserId"]); if (formCollection["SubCategoryId"] != "undefined") { model.SubCategoryId = Convert.ToInt64(formCollection["SubCategoryId"]); } if (formCollection["Price"] != "null") { model.Price = Convert.ToDecimal(formCollection["Price"]); } if (formCollection["ClientPrice"] != "null") { model.ClientPrice = Convert.ToDecimal(formCollection["ClientPrice"]); } var json = formCollection["Attachments"].ToString(); List <string> getAttachments = Newtonsoft.Json.JsonConvert.DeserializeObject <List <string> >(json); model.ReferenceImages = new List <string>(); var date = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss").Replace("-", "_"); if (getAttachments.Count > 0) { var count = getAttachments.Count(); if (count > 0) { for (int i = 0; i < getAttachments.Count(); i++) { string imageName = "JobRequest" + Guid.NewGuid() + date + ".jpg"; string[] splitAttachment = getAttachments[i].Split(','); Common.SaveImage(splitAttachment[1], imageName); model.ReferenceImages.Add(imageName); } } } if (formCollection["SubSubCategories"] != null) { var subsubCatArr = formCollection["SubSubCategories"].ToString(); model.SubSubCategories = Newtonsoft.Json.JsonConvert.DeserializeObject <List <long> >(subsubCatArr); } var status = propertyService.AddJobRequest(model); wsBase obj = new wsBase() { status = status, message = propertyService.message }; return(Json(obj, JsonRequestBehavior.AllowGet)); }