使用一种方法我正在执行所有的crud操作, 控制器代码: -
public JsonResult AddEmployee(EmployeeModel Emp)
{
#region Object Declaration
SqlConnection connection = null;
SqlCommand com = null;
DataSet ds = null;
#endregion
try
{
// Get Connection string
string connectionString = GetConnectionString();
// Set sql connection
connection = new SqlConnection(connectionString);
// Set sql commands
com = new SqlCommand("SP_tblInsertEmployee_Insert", connection);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@EmployeeName", Emp.EmployeeName);
com.Parameters.AddWithValue("@Designation", Emp.Designation);
com.Parameters.AddWithValue("@MobileNo", Emp.MobileNo);
connection.Open();
int a = com.ExecuteNonQuery();
connection.Close();
//Code For GetData
com = new SqlCommand("SP_tblInsertEmployee_SelectAllDatabyID", connection);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@EmployeeID",a);
connection.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
ds = new DataSet();
da.Fill(ds);
int c = com.ExecuteNonQuery();
connection.Close();
}
catch (Exception ex)
{
}
finally
{
#region Release Object
connection = null;
com = null;
#endregion
}
return Json(ds,JsonRequestBehavior.AllowGet);
}
JavaScript代码: -
function Bind_EmployeeDetails_Grid(data)
{
$("#tblEmployeeDetails").html("");
var htmltext = "";
$("txtEmployeeID").val(data.Emp.EmployeeID),
$("txtEmployeeName").val(data.Emp.EmployeeName),
$("txtDesignation").val(data.Emp.Designation),
$("txtMobileNo").val(data.Emp.MobileNo)
if(data.Emp.length > 0)
{
htmltext += "<tr>";
htmltext += "<th>EmployeeID</th>";
htmltext += "<th>EmployeeName</th>";
htmltext += "<th>Designation</th>";
htmltext += "<th>MobileNo</th>";
htmltext += "</tr>";
}
for(i=0; i<data.Emp.length; i++)
{
htmlText += "<tr>";
htmlText += "<td>";
htmlText += data.Emp[i].EmployeeID == null ? "" : data.Emp[i].EmployeeID;
htmlText += "</td>";
htmlText += "<td>";
htmlText += data.Emp[i].EmployeeName == null ? "" : data.Emp[i].EmployeeName;
htmlText += "</td>";
htmlText += "<td>";
htmlText += data.Emp[i].Designation == null ? "" : data.Emp[i].Designation;
htmlText += "</td>";
htmlText += "<td>";
htmlText += data.Emp[i].MobileNo == null ? "" : data.Emp[i].MobileNo;
htmlText += "</td>";
}
$('#tblEmployeeDetails').html(htmltext);
// ClearData();
}