/* commented methods which send JSON data * // returns all customers * public ActionResult getCustomers() // JSON Collection * { * // fill the customers collections * Dal.Dal dal = new Dal.Dal(); * List<Customer> customersColl = dal.Customers.ToList<Customer>(); * * // Delay for Synchronous execution (10sec) * //Thread.Sleep(3000); * * return Json(customersColl, JsonRequestBehavior.AllowGet); * } * * // returns the Customer for entered Customor Name * [ActionName("getCustomerByName")] * public ActionResult getCustomers(Customer objCustomer) // JSON Collection * { * // fill the customers collections * Dal.Dal dal = new Dal.Dal(); * List<Customer> customersColl = (from c in dal.Customers * where c.CustomerName == objCustomer.CustomerName * select c).ToList<Customer>(); * * // Delay for Synchronous execution (10sec) * // Thread.Sleep(3000); * * return Json(customersColl, JsonRequestBehavior.AllowGet); * } * * * // return the Customer for entered Customor Name * // returns View - SearchCustomer * public ActionResult SearchCustomer() * { * // View Model object * CustomerViewModel obj = new CustomerViewModel(); * * // fill the customers collections * Dal.Dal dal = new Dal.Dal(); * // to fill all customers * // List<Customer> customersColl = dal.Customers.ToList<Customer>(); * * string str = Request.Form["txtCustomerName"].ToString(); * * // to filter customers with conditions * List<Customer> customersColl * = (from x in dal.Customers * where x.CustomerName == str * select x).ToList<Customer>(); * obj.customers = customersColl; * * return View("SearchCustomer", obj); * } * */ public ActionResult Submit(Customer obj) // validation runs { //// no need - as JSON object is being passed from front end //// manual binding of object with form elements //Customer obj = new Customer(); //obj.CustomerName = Request.Form["customer.CustomerName"]; //obj.CustomerCode = Request.Form["customer.CustomerCode"]; //// no need - end if (ModelState.IsValid) { // insert the Customer object to database // EF DAL Dal.Dal Dal = new Dal.Dal(); Dal.Customers.Add(obj); // in memory Dal.SaveChanges(); // physical commit //return View("Customer", obj); } // fill the customers collections Dal.Dal dal = new Dal.Dal(); List <Customer> customersColl = dal.Customers.ToList <Customer>(); //return View("EnterCustomer",vm); // removed as JSON will be sent return(Json(customersColl, JsonRequestBehavior.AllowGet)); }
// Insert public /*List<Customer>*/ Object Post(Customer objCustomer) // Object - to avoid typecasting error (Error to Customers) { if (ModelState.IsValid) { // insert the Customer object to database // EF DAL Dal.Dal Dal = new Dal.Dal(); Dal.Customers.Add(objCustomer); // in memory Dal.SaveChanges(); // physical commit } else // get all the errors in ModelState and put it in Error collection { var Err = new Error(); Err.Errors = new List <string>(); // to avoid null reference in Err.Errors.Add foreach (var modelState in ModelState) { foreach (var error in modelState.Value.Errors) { Err.Errors.Add(error.ErrorMessage); } } objClientData.isValid = false; objClientData.data = Err; return(objClientData); } // fill the customers collections Dal.Dal dal = new Dal.Dal(); List <Customer> customersColl = dal.Customers.ToList <Customer>(); //return customersColl; // WebAPI will decide the content type on its own (HTML, JSON, JPEG...) objClientData.isValid = true; objClientData.data = customersColl; return(objClientData); // WebAPI will decide the content type on its own (HTML, JSON, JPEG...) }
public List <Book> Post(Book obj) { if (ModelState.IsValid) { Dal.Dal Dal = new Dal.Dal(); Dal.Books.Add(obj); Dal.SaveChanges(); } Dal.Dal dal = new Dal.Dal(); List <Book> booksList = dal.Books.ToList <Book>(); return(booksList); }
public List <Book> Delete(Book obj) { // Select the record Dal.Dal Dal = new Dal.Dal(); Book custdelete = (from temp in Dal.Books where temp.bookGenre == obj.bookGenre select temp).ToList <Book>()[0]; // Remove Dal.Books.Remove(custdelete); Dal.SaveChanges(); List <Book> booksList = Dal.Books.ToList <Book>(); return(booksList); }
// Delete public List <Customer> Delete(Customer objCustomer) { // Select the record using LINQ Dal.Dal objDal = new Dal.Dal(); Customer custDelete = (from c in objDal.Customers where c.CustomerCode == objCustomer.CustomerCode select c).ToList <Customer>()[0]; objDal.Customers.Remove(custDelete); objDal.SaveChanges(); List <Customer> customerColl = objDal.Customers.ToList <Customer>(); return(customerColl); }