#javascript #c# #jquery #json
#JavaScript #c# #jquery #json
Вопрос:
Используя один метод, я выполняю все операции 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();
}
Комментарии:
1. вы не закрываете свой
<tr>
с помощью a</tr>
во втором цикле. Это может быть проблемой2. Ваш веб-метод возвращает a
DataSet
как json. И в javascript, к которому вы пытаетесь получить доступdata.Emp
, содержит ли ваш набор данных таблицу с именемEmp
?