public IEnumerable <ContactModel> GetFetchContact(int contactID) { Contact contact = null; List <ContactModel> contacts = new List <ContactModel>(); List <ContactModel> FetchedContact = new List <ContactModel>(); try { MYCONTACTBOOKEntities dc = new MYCONTACTBOOKEntities(); var v = (from a in dc.Contacts join b in dc.Countries on a.CountryID equals b.CountryID join c in dc.States on a.StateID equals c.StateID select new ContactModel { ContactID = a.ContactID, FirstName = a.ContactPersonFname, LastName = a.ContactPersonLname, ContactNo1 = a.ContactNo1, ContactNo2 = a.ContactNo2, EmailID = a.EmailID, Country = b.CountryName, State = c.StateName, Address = a.Address, ImagePath = a.ImagePath }).ToList(); contacts = v; FetchedContact = contacts.Where(x => x.ContactID.Equals(contactID)).ToList(); return(FetchedContact); } catch (Exception ex) { return(FetchedContact); } }
public IHttpActionResult DeleteProduct(int id) { try { if (id <= 0) { return(BadRequest("Not a valid contact id")); } using (var ctx = new MYCONTACTBOOKEntities()) { var contact = ctx.Contacts .Where(c => c.ContactID == id) .FirstOrDefault(); ctx.Entry(contact).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } } catch (Exception) { return(BadRequest("Not a valid contact id")); } return(Ok()); }
public IEnumerable <ContactModel> GetContactbyId(string id) { List <ContactModel> contacts = new List <ContactModel>(); List <ContactModel> searchedContacts = new List <ContactModel>(); try { MYCONTACTBOOKEntities dc = new MYCONTACTBOOKEntities(); var v = (from a in dc.Contacts join b in dc.Countries on a.CountryID equals b.CountryID join c in dc.States on a.StateID equals c.StateID select new ContactModel { ContactID = a.ContactID, FirstName = a.ContactPersonFname, LastName = a.ContactPersonLname, ContactNo1 = a.ContactNo1, ContactNo2 = a.ContactNo2, EmailID = a.EmailID, Country = b.CountryName, State = c.StateName, Address = a.Address, ImagePath = a.ImagePath }).ToList(); contacts = v; searchedContacts = contacts.Where(x => x.FirstName.ToLower().Contains(id.ToLower()) || x.LastName.ToLower().Contains(id.ToLower())).ToList(); return(searchedContacts); } catch (Exception ex) { return(searchedContacts); } }
public IHttpActionResult PostNewContact(Contact c) { try { if (!ModelState.IsValid) { return(BadRequest("Not a valid model")); } using (var ctx = new MYCONTACTBOOKEntities()) { ctx.Contacts.Add(new Contact() { ContactPersonFname = c.ContactPersonFname, ContactPersonLname = c.ContactPersonLname, ContactNo1 = c.ContactNo1, ContactNo2 = c.ContactNo1, EmailID = c.EmailID, CountryID = c.CountryID, StateID = c.StateID, Address = c.Address, ImagePath = c.ImagePath }); ctx.SaveChanges(); } } catch (Exception ex) { return(BadRequest("Not a valid model")); } return(Ok()); }
public ActionResult Add() { //fetch country data List <Country> AllCountry = new List <Country>(); List <State> states = new List <State>(); using (MYCONTACTBOOKEntities dc = new MYCONTACTBOOKEntities()) { AllCountry = dc.Countries.OrderBy(a => a.CountryName).ToList(); //Not need to fetch state as we dont know which country will user select here } ViewBag.Country = new SelectList(AllCountry, "CountryID", "CountryName"); ViewBag.State = new SelectList(states, "StateID", "StateName"); return(View()); }
public ActionResult GetStates(int countryID) { using (MYCONTACTBOOKEntities dc = new MYCONTACTBOOKEntities()) { //We will off Lazy Loading var State = (from a in dc.States where a.CountryID.Equals(countryID) orderby a.StateName select a).ToList(); var result = (from s in State select new { StateID = s.StateID, StateName = s.StateName }).ToList(); return(Json(result, JsonRequestBehavior.AllowGet)); } }
public ActionResult Add(Contact c, HttpPostedFileBase file) { try { #region // Fetch Country & State List <Country> allCountry = new List <Country>(); List <State> states = new List <State>(); using (MYCONTACTBOOKEntities dc = new MYCONTACTBOOKEntities()) { allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList(); if (c.CountryID > 0) { states = dc.States.Where(a => a.CountryID.Equals(c.CountryID)).OrderBy(a => a.StateName).ToList(); } } ViewBag.Country = new SelectList(allCountry, "CountryID", "CountryName", c.CountryID); ViewBag.State = new SelectList(states, "StateID", "StateName", c.StateID); #endregion #region// Validate file if selected if (file != null) { if (file.ContentLength > (512 * 1000)) // 512 KB { ModelState.AddModelError("FileErrorMessage", "File size must be within 512 KB"); } string[] allowedType = new string[] { "image/png", "image/gif", "image/jpeg", "image/jpg" }; bool isFileTypeValid = false; foreach (var i in allowedType) { if (file.ContentType == i.ToString()) { isFileTypeValid = true; break; } } if (!isFileTypeValid) { ModelState.AddModelError("FileErrorMessage", "Only .png, .gif and .jpg file type allowed"); } } #endregion #region// Validate Model & Save to Database if (ModelState.IsValid) { //Save here if (file != null) { string savePath = Server.MapPath("~/Image"); string fileName = Guid.NewGuid() + Path.GetExtension(file.FileName); file.SaveAs(Path.Combine(savePath, fileName)); c.ImagePath = fileName; } ServiceRepository serviceObj = new ServiceRepository(); HttpResponseMessage response = serviceObj.PostResponse("api/Values/", c); if (response.IsSuccessStatusCode) { response.EnsureSuccessStatusCode(); return(RedirectToAction("GetContactlist")); } return(View(c)); } else { ModelState.AddModelError(string.Empty, "Please capture all the required fileds."); return(View(c)); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.ToString()); return(View(c)); } #endregion }