Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • LINQ Delete in MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 35
    Comment on it

    While working in MVC we use LINQ to make our task easier and to make the manipulations easier

     

    First we will create the action for doing this task.

     

    
            [HttpPost]
            public JsonResult Remove(int id)
            {
                new GridModel().Remove(id);
                return Json(true);
            }
    

     

    After this we will create a class that will perform the implementation of the LINQ query.

     

    Before doing this we need to create the DataContext for this model.

     

     public enum getstatus
        {
            success, failure
        }
        public class EmployeeModel
        {
    
            public int EmpId { get; set; }
            [Required(ErrorMessage = "First Name is required")]
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            [Required(ErrorMessage = "Last Name is required")]
            public string LastName { get; set; }
            [Required(ErrorMessage = "PhoneNo is required")]
            public string PhoneNo { get; set; }
            public string Address { get; set; }
            public getstatus Status { get; set; }
            public string msg { get; set; }
        }

     

     

     

    After doing this we can use this context model object to delete the records from the database.

     

       public void Remove(int id)
            {
                var deletedproducts = from p in DataContextObj.EmployeeDetails
                                      where p.EmpId == id
                                      select p;
                DataContextObj.EmployeeDetails.DeleteAllOnSubmit(deletedproducts);
    
                DataContextObj.SubmitChanges();
            }

     

     

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: