In MVC everything is based on the concept of action contained in the controller and the view that is being generated for that action.
While performing deletion in the gridview you first need to make the model class that will access the details from the database and display into the gridview.
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 will create the action that is used to delete the view.
[HTTPPost]
public ActionResult Delete(int id)
{
EmployeeModel emp = new EmployeeModel();
emp.EmpId = id;
EmpRepObj = new EmployeeRepository();
emp = EmpRepObj.DeleteDetails(emp);
return View(emp);
}
Then we will create a method in the repository that will delete the details in the repository.
public string DeleteUser(int EmpId)
{
string msg = null;
try
{
int result = -1;
using (SqlConnection sqlConnection = new SqlConnection(strConnString))
{
List<SqlParameter> parameterList = new List<SqlParameter>();
parameterList.Add(new SqlParameter("@empId", EmpId));
if (String.Equals(sqlConnection.State, ConnectionState.Closed))
sqlConnection.Open();
result = Convert.ToInt32(SqlHelper.ExecuteScalar(strConnString, CommandType.StoredProcedure, "uspDeleteUser", parameterList.ToArray()));
//EmpMod.Status = (result > 0 ? getstatus.success : getstatus.failure);
msg =Convert.ToString(getstatus.success);
}
}
catch (SqlException ex)
{
msg = ex.Message;
}
return msg;
}
0 Comment(s)