public IHttpActionResult AddVolunteer(VolunteerWork volunteer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.VolunteerWorks.Add(volunteer); try { db.SaveChanges(); } catch (DbUpdateException) { return(Ok("error")); } return(Ok("success")); }
public async Task <ActionResult> Volunteer(FormCollection collection) { if (Session["user_type"].Equals("GST") || Session["user_type"].Equals("CMP")) { return(RedirectToAction("/Home/Index")); } VolunteerWork w = new VolunteerWork(); if (string.IsNullOrWhiteSpace(Request.Form["cname-volunteer"]) && !string.IsNullOrWhiteSpace(Request.Form["volunteer"])) { w.Name = Request.Form["volunteer"]; } else if (!string.IsNullOrWhiteSpace(Request.Form["cid-volunteer"])) { w.GoogleId = Request.Form["cid-volunteer"]; w.Name = Request.Form["cname-volunteer"]; } else { return(RedirectToAction("../Student/Details/" + Session["user_id"])); } if (string.IsNullOrWhiteSpace(Request.Form["sdate"]) || string.IsNullOrWhiteSpace(Request.Form["topic"])) { return(RedirectToAction("../Student/Details/" + Session["user_id"])); } if (string.IsNullOrWhiteSpace(Request.Form["edate"])) { w.EndDate = null; } else { w.EndDate = Convert.ToDateTime(Request.Form["edate"]); } w.S_Id = Session["user_id"].ToString(); w.StartDate = Convert.ToDateTime(Request.Form["sdate"]); w.Topic = Request.Form["topic"]; try { string response = ""; using (var client = new HttpClient()) { client.BaseAddress = new Uri(Baseurl); client.DefaultRequestHeaders.Clear(); //Define request data format client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); //Sending request to find web api REST service resource GetAllEmployees using HttpClient HttpResponseMessage Res = await client.PostAsJsonAsync("api/extraAPI/Volunteer", w); //Checking the response is successful or not which is sent using HttpClient if (Res.IsSuccessStatusCode) { //Storing the response details recieved from web api var StudentResponse = Res.Content.ReadAsStringAsync().Result; //Deserializing the response recieved from web api and storing into the Employee list response = JsonConvert.DeserializeObject <string>(StudentResponse); } } return(RedirectToAction("../Student/Details/" + Session["user_id"])); } catch { return(RedirectToAction("../Student/Details/" + Session["user_id"])); } }