我從存儲過程結果中獲取多個表。我想把這個全部表作爲asp.netNet MVC中的JsonResult作爲ajax響應返回。但我沒有在console.log(response);
得到回覆。如何在MVC控制器中將多個表作爲JsonResult返回?如何在asp.net MVC中將多個表返回爲jsonreult?
我調試進入方法和存儲過程中獲取數據並返回爲JSON但隨後後控制檯我得到錯誤的波紋管,
在Ajax請求,我有打電話GetKYCListForUpdate
方法如波紋管,
$('#KycListTable tbody').on('click', '.btn-edit', function (e) {
var data = table.row($(this).parents('tr')).data();
if (data.PartyBillId != null) {
$.ajax({
type: "POST",
url: "@Url.Action("GetKYCListForUpdate", "KYCRegistration")",
data: { id: data.PartyBillId },
success: function (response) {
debugger
console.log(response);
var result = response.aaData;
if (result != null) {
$.each(result, function (i, item) {
console.log(item)
});
}
}
});
}
});
在asp.net MVC的控制器方法如波紋管
,
public JsonResult GetKYCListForUpdate(int id)
{
var AuthId = NC.ToInt(((Variables.scUserDetail)Session["UserDetail"]).AuthId);
SqlConnection objsql = default(SqlConnection);
objsql = new SqlConnection(Connect.GetConnStr(1));
DataSet dsData = new DataSet();
SqlCommand objSelectCmd = new SqlCommand();
try
{
//objsql.ConnectionString = objsql;
objsql.Open();
objSelectCmd.CommandType = CommandType.StoredProcedure;
objSelectCmd.Connection = objsql;
objSelectCmd.CommandText = "Sp_M35_01_GetKycListToEdit";
objSelectCmd.Parameters.AddWithValue("@BillPartyId", id);
objSelectCmd.Parameters.AddWithValue("@AuthId", AuthId);
SqlDataAdapter objDataAdapter = new SqlDataAdapter();
objDataAdapter = new SqlDataAdapter(objSelectCmd);
objDataAdapter.Fill(dsData);
for (int i = 0; i < dsData.Tables.Count; i++)
{
if (dsData.Tables[i].Rows.Count > 0)
{
result.Add(GetTableRows(dsData.Tables[i]));
}
}
return Json(new { aaData = result }, JsonRequestBehavior.AllowGet);
}
public List<Dictionary<string, object>> GetTableRows(DataTable dtData)
{
List<Dictionary<string, object>>
lstRows = new List<Dictionary<string, object>>();
Dictionary<string, object> dictRow = null;
foreach (DataRow dr in dtData.Rows)
{
dictRow = new Dictionary<string, object>();
foreach (DataColumn col in dtData.Columns)
{
dictRow.Add(col.ColumnName, dr[col]);
}
lstRows.Add(dictRow);
}
return lstRows;
}
我想'VAR數據= table.row($(本)。家長( 'TR'))。 data();'需要:var data = $(this).parent('tr')。data();' –
根據錯誤,Action方法'GetKYCListForUpdate'沒有獲得ID參數。檢查data.PartyBillId是否爲有效num – TechGirl
正如@ TechGirl所說,錯誤指出你沒有向控制器發送'id' oparameter。所以請檢查你發送的內容。 –