Exemplo n.º 1
0
 [HttpGet] // Set the attribute to Read
 public ActionResult Read()
 {
     using (var context = new CRUDDemo1Entities1())
     {
         var data = context.Tables.ToList(); // Return the list of data from the database
         return(View(data));
     }
 }
Exemplo n.º 2
0
 public ActionResult Update(int Studentid) // To fill data in the form to enable easy editing
 {
     using (var context = new CRUDDemo1Entities1())
     {
         var data = context.Tables.Where(x => x.StudentNo == Studentid).SingleOrDefault();
         return(View(data));
     }
 }
Exemplo n.º 3
0
        [HttpPost]    //Specify the type of attribute i.e. it will add the record to the database
        public ActionResult create(Table model)
        {
            using (var context = new CRUDDemo1Entities1()) //To open a connection to the database
            {
                context.Tables.Add(model);                 // Add data to the particular table
                context.SaveChanges();                     // save the changes to the that are made
            }
            string message = "Created the record successfully";

            ViewBag.Message = message; // To display the message on the screen after the record is created successfully
            return(View());            // write @Viewbag.Message in the created view at the place where you want to display the message
        }
Exemplo n.º 4
0
 public ActionResult Delete(int Studentid)
 {
     using (var context = new CRUDDemo1Entities1())
     {
         var data = context.Tables.FirstOrDefault(x => x.StudentNo == Studentid);
         if (data != null)
         {
             context.Tables.Remove(data);
             context.SaveChanges();
             return(RedirectToAction("Read"));
         }
         else
         {
             return(View());
         }
     }
 }
Exemplo n.º 5
0
 [ValidateAntiForgeryToken] // To specify that this will be invoked when post method is called
 public ActionResult Update(int Studentid, Table model)
 {
     using (var context = new CRUDDemo1Entities1())
     {
         var data = context.Tables.FirstOrDefault(x => x.StudentNo == Studentid); // Use of lambda expression to access particular record from a database
         if (data != null)                                                        // Checking if any such record exist
         {
             data.Name    = model.Name;
             data.Section = model.Section;
             data.Email   = model.Email;
             data.Branch  = model.Branch;
             context.SaveChanges();
             return(RedirectToAction("Read")); // It will redirect to the Read method
         }
         else
         {
             return(View());
         }
     }
 }