// GET: Student/Create public ActionResult Create() { if (Session["user_type"].Equals("STD") || Session["user_type"].Equals("CMP")) { return(RedirectToAction("../Home/Index")); } StudentRegErr err = new StudentRegErr(); return(View(err)); }
public async Task <ActionResult> Create(FormCollection collection) { if (Session["user_type"].Equals("STD") || Session["user_type"].Equals("CMP")) { return(RedirectToAction("../Home/Index")); } bool flag = false; StudentRegErr err = new StudentRegErr(); Student s = new Student(); s.FirstName = Request.Form["fname"]; s.LastName = Request.Form["lname"]; s.Email = Request.Form["email"]; s.Password = Request.Form["password"]; s.Gender = Request.Form["gender"]; HttpPostedFileBase cardfront = Request.Files["cardfront"]; if ((cardfront != null) && (cardfront.ContentLength > 0) && !string.IsNullOrWhiteSpace(cardfront.FileName)) { byte[] cardfrontBytes = new byte[cardfront.ContentLength]; cardfront.InputStream.Read(cardfrontBytes, 0, Convert.ToInt32(cardfront.ContentLength)); s.CardFront = cardfrontBytes; } else { flag = true; err.CardFront = "You must upload a pic of the FRONT of your id card as a .jpg or a .png file"; } HttpPostedFileBase cardback = Request.Files["cardback"]; if ((cardback != null) && (cardback.ContentLength > 0) && !string.IsNullOrWhiteSpace(cardback.FileName)) { byte[] cardbackBytes = new byte[cardback.ContentLength]; cardback.InputStream.Read(cardbackBytes, 0, Convert.ToInt32(cardback.ContentLength)); s.CardBack = cardbackBytes; } else { flag = true; err.CardBack = "You must upload a pic of the BACK of your id card as a .jpg or a .png file"; } err.s = s; DateTime result; if (string.IsNullOrWhiteSpace(Request.Form["dob"]) || !DateTime.TryParse(Request.Form["dob"], out result)) { flag = true; err.DOB = "Date of Birth Cannot be Empty"; } else { s.DOB = Convert.ToDateTime(Request.Form["dob"]); } if (string.IsNullOrWhiteSpace(s.Email) || !Regex.IsMatch(s.Email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase)) { flag = true; err.Email = "Email Cannot be empty. It should be a valid email address."; } if (!string.IsNullOrWhiteSpace(Request.Form["mobile"])) { s.Mobile = Request.Form["mobile"]; } if (string.IsNullOrWhiteSpace(s.FirstName)) { flag = true; err.FirstName = "First Name Cannot be Empty"; } if (string.IsNullOrWhiteSpace(s.LastName)) { flag = true; err.LastName = "Last Name Cannot be Empty"; } if (string.IsNullOrWhiteSpace(s.Password)) { flag = true; err.Password = "******"; } if (!s.Password.Equals(Request.Form["repassword"])) { flag = true; err.RePassword = "******"; } if (flag) { return(View(err)); } 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/studentsAPI/", s); //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); } } if (response.Equals("email exists")) { err.Email = "This Email Id Already Exists"; return(View(err)); } else { Session["user_type"] = "STD"; Session["user_id"] = response; return(RedirectToAction("../Home/Index")); } } catch { return(View(err)); } }