示例#1
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
        }
示例#2
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());
         }
     }
 }
示例#3
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());
         }
     }
 }