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();
}
0 Comment(s)