public ActionResult Create(FormCollection collection) { try { if (ModelState.IsValid) { //first create a new object using form collection //then pass the object to model to create. TODOItem newTODOItem = new TODOItem(); newTODOItem.Description = collection["Description"]; newTODOItem.Status = collection["Status"]; //Lets invoke DB entity using (TODOITEMEntity tODOITEMEntity = new TODOITEMEntity()) { tODOITEMEntity.TODOItems.Add(newTODOItem); tODOITEMEntity.SaveChanges(); return(RedirectToAction("Index")); } } return(RedirectToAction("Index")); } catch { //Handle catch situation return(View()); } }
public ActionResult Delete(int id) { try { //let's grab the object using passed id //pass that object to be removed from db using (TODOITEMEntity tODOITEMEntity = new TODOITEMEntity()) { var itemToBeDeleted = tODOITEMEntity.TODOItems.SingleOrDefault(item => item.Id == id); if (itemToBeDeleted == null) { //throw a exception } //else tODOITEMEntity.TODOItems.Remove(itemToBeDeleted); tODOITEMEntity.SaveChanges(); return(RedirectToAction("Index")); } } catch { //handle return(RedirectToAction("Index")); } }
public ActionResult Edit(FormCollection collection) { try { using (TODOITEMEntity tODOITEMEntity = new TODOITEMEntity()) { //let's grab checked items //loop thru items that were checked. // pass them to model to update status. string[] idArray = new string[] { }; string[] checkArray = new string[] { }; foreach (var key in collection.AllKeys) { if (key.ToLower().Contains("id")) { idArray = collection[key].Split(','); } if (key.Contains("itemChk")) { //first replace "true,false" with only true as form submit //true followed by false for check box checkArray = collection[key].Replace("true,false", "true").Split(','); } } int checkArrayLenght = checkArray.Length; //run a loop on check array for (int i = 0; i < checkArrayLenght; i++) { int ID = Convert.ToInt32(idArray[i]); //check if check box was selected if (checkArray[i].ToLower() == "true") { TODOItem itemToBeUpdated = tODOITEMEntity.TODOItems.SingleOrDefault(item => item.Id == ID); itemToBeUpdated.Status = "Completed"; } } tODOITEMEntity.SaveChanges(); return(RedirectToAction("Index")); } } catch { return(RedirectToAction("Error")); } }