Ejemplo n.º 1
0
 public ActionResult Update(int Productid)
 {
     using (var context = new demoCRUDEntities())
     {
         var data = context.Product.Where(x => x.ProductNo == Productid).SingleOrDefault();
         return(View(data));
     }
 }
Ejemplo n.º 2
0
 public ActionResult Update(int Studentid) // To fill data in the form to enable easy editing
 {
     using (var context = new demoCRUDEntities())
     {
         var data = context.Student.Where(x => x.StudentNo == Studentid).SingleOrDefault();
         return(View(data));
     }
 }
Ejemplo n.º 3
0
 public ActionResult Read()
 {
     using (var context = new demoCRUDEntities())
     {
         var data = context.Product.ToList();
         return(View(data));
     }
 }
Ejemplo n.º 4
0
 [HttpGet] // Set the attribute to Read
 public ActionResult Read()
 {
     using (var context = new demoCRUDEntities())
     {
         var data = context.Student.ToList(); // Return the list of data from the database
         return(View(data));
     }
 }
Ejemplo n.º 5
0
        public ActionResult create(Product Productname)
        {
            using (var context = new demoCRUDEntities())
            {
                context.Product.Add(Productname);
                context.SaveChanges();
            }
            string message = "Created the record successfully";

            ViewBag.Message = message;
            return(View());
        }
Ejemplo n.º 6
0
        [HttpPost]    //Specify the type of attribute i.e. it will add the record to the database
        public ActionResult create(Student model)
        {
            using (var context = new demoCRUDEntities()) //To open a connection to the database
            {
                context.Student.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
        }
Ejemplo n.º 7
0
 public bool DeleteStudent(int studentNo)
 {
     using (var context = new demoCRUDEntities())
     {
         var student = context.Student.FirstOrDefault(x => x.StudentNo == studentNo);
         if (student != null)
         {
             context.Student.Remove(student);
             context.SaveChanges();
             return(true);
         }
         return(false);
     }
 }
Ejemplo n.º 8
0
 public ActionResult Delete(int Productid)
 {
     using (var context = new demoCRUDEntities())
     {
         var data = context.Product.FirstOrDefault(x => x.ProductNo == Productid);
         if (data != null)
         {
             context.Product.Remove(data);
             context.SaveChanges();
             return(RedirectToAction("Read"));
         }
         else
         {
             return(View());
         }
     }
 }
Ejemplo n.º 9
0
        public List <StudentsModel> GetAllStudents()
        {
            using (var context = new demoCRUDEntities())
            {
                var result = context.Student
                             .Select(x => new StudentsModel()
                {
                    Name      = x.Name,
                    Branch    = x.Branch,
                    StudentNo = x.StudentNo,
                    Section   = x.Section,
                    EmailId   = x.EmailId
                }).ToList();

                return(result);
            }
        }
Ejemplo n.º 10
0
        public StudentsModel GetStudent(int StudentNo)
        {
            using (var context = new demoCRUDEntities())
            {
                var result = context.Student
                             .Where(x => x.StudentNo == StudentNo)
                             .Select(x => new StudentsModel()
                {
                    Name      = x.Name,
                    Branch    = x.Branch,
                    StudentNo = x.StudentNo,
                    Section   = x.Section,
                    EmailId   = x.EmailId
                }).FirstOrDefault();

                return(result);
            }
        }
Ejemplo n.º 11
0
        public bool UpdateStudents(int StudentNo, StudentsModel model)
        {
            using (var context = new demoCRUDEntities())
            {
                var student = context.Student.FirstOrDefault(x => x.StudentNo == StudentNo);

                if (student != null)
                {
                    student.Section = model.Section;
                    student.Branch  = model.Branch;
                    student.EmailId = model.EmailId;
                    student.Name    = model.Name;
                }

                context.SaveChanges();

                return(true);
            }
        }
Ejemplo n.º 12
0
 public ActionResult Update(int Productid, Product Productname)
 {
     using (var context = new demoCRUDEntities())
     {
         var data = context.Product.FirstOrDefault(x => x.ProductNo == Productid);
         if (data != null)
         {
             data.Name         = Productname.Name;
             data.Id           = Productname.Id;
             data.Productname  = Productname.Productname;
             data.Categoryname = Productname.Categoryname;
             context.SaveChanges();
             return(RedirectToAction("Read"));
         }
         else
         {
             return(View());
         }
     }
 }
Ejemplo n.º 13
0
 [ValidateAntiForgeryToken] // To specify that this will be invoked when post method is called
 public ActionResult Update(int Studentid, Student model)
 {
     using (var context = new demoCRUDEntities())
     {
         var data = context.Student.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.EmailId = model.EmailId;
             data.Branch  = model.Branch;
             context.SaveChanges();
             return(RedirectToAction("Read")); // It will redirect to the Read method
         }
         else
         {
             return(View());
         }
     }
 }
Ejemplo n.º 14
0
        public int AddStudent(StudentsModel model)
        {
            using (var context = new demoCRUDEntities())
            {
                Student stu = new Student()
                {
                    Name      = model.Name,
                    Branch    = model.Branch,
                    Section   = model.Section,
                    StudentNo = model.StudentNo,
                    EmailId   = model.EmailId
                };

                context.Student.Add(stu);

                context.SaveChanges();

                return(stu.StudentNo);
            }
        }