[英]Not Returning To AJAX Success From C# Controller Action
無法從C#控制器操作返回到AJAX成功我嘗試了許多可用的選項,但是它仍然沒有將數據返回給AJAX成功,這可能是什么原因? 我想將列表Listemr_t_Immunization
從控制器的操作返回給我的AJAX調用的成功數據,我該怎么做
請看下面的代碼謝謝
AJAX請求
$.ajax(
{
url: '@Url.Action("getmedi", "Immunization")',
type: 'Post',
async: false,
contentType: 'application/json',
dataType: "json",
data: JSON.stringify({ "patient2": patient2}),
success: function (data) {
debugger
$(data).each(function(index, element){
$('#first > tbody').append('<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>');
$('#first > tbody > tr:last-child > td:nth-child(1)').append('<input id='+element.ImmunizationId+' value='+element.VaccineName+'>');
$('#first > tbody > tr:last-child > td:nth-child(2)').append('<input id='+element.Immunization_Id+' value='+element.DateGiven+'>');
$('#first > tbody > tr:last-child > td:nth-child(3)').append('<input id='+element.Immunization_Id+' value='+element.Route+'>');
})
$("input[name='VaccineName']").attr('disabled', 'disabled');
$("input[name='DateGiven']").attr('disabled', 'disabled');
$("input[name='Route']").attr('disabled', 'disabled');
},
error: function (textStatus, errorThrown)
{
debugger
}
});
控制器動作
[HttpPost]
public JsonResult getmedi(int patient2)
{
if (patient2.ToString() != "")
{
string roster = objOrgCont.getmedi(patient2);
JavaScriptSerializer ser = new JavaScriptSerializer();
emr_t_Immunization erx = (emr_t_Immunization)ser.Deserialize(roster, typeof(emr_t_Immunization));
List<emr_t_Immunization> Listemr_t_Immunization = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList();
///List<emr_t_Medical_History> Listemr_t_Medical_History2 = (from Allemr_t_Medical_History in db.emr_t_Medical_History where Allemr_t_Medical_History.Mr_No == Mr_No select Allemr_t_Medical_History).ToList();
if (erx != null)
{
//return Json(new { success = true, for_Date = erx.Med_Date, for_Name = erx.Name, for_Active = erx.Active, for_Resolved = erx.Resolved, for_Comments=erx.Comments });
return Json(new { Listemr_t_Immunization, JsonRequestBehavior.DenyGet });
}
}
return Json(new { success = false });
}
我還試圖在返回之前將其字符串化,並且顯示以下錯誤
我已經使用return JsonConvert.SerializeObject(Listemr_t_Immunization);
現在它給我錯誤
使用類型'System.Data.Entity.DynamicProxies.as_t_appointment_305685F58BEE75BAC95975F9457412A0DE58BB59D3EDBD1155C7DB5E21A7BA66'檢測到自引用循環。 路徑'[0] .erx_t_patient.as_t_appointment [0] .erx_t_city.as_t_appointment'
乍一看,除非您的條件之一失敗,否則您似乎可以將失敗的返回值移動到else語句中,以防止它們被解雇,而不僅僅是自己漂浮在外面。
每次回叫失敗的問題嗎? 如果是這樣,那可能就是原因。
[HttpPost]
public JsonResult getmedi(int patient2)
{
if (patient2.ToString() != "")
{
string roster = objOrgCont.getmedi(patient2);
JavaScriptSerializer ser = new JavaScriptSerializer();
emr_t_Immunization erx = (emr_t_Immunization)ser.Deserialize(roster, typeof(emr_t_Immunization));
List<emr_t_Immunization> Listemr_t_Immunization = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList();
///List<emr_t_Medical_History> Listemr_t_Medical_History2 = (from Allemr_t_Medical_History in db.emr_t_Medical_History where Allemr_t_Medical_History.Mr_No == Mr_No select Allemr_t_Medical_History).ToList();
if (erx != null)
{
//return Json(new { success = true, for_Date = erx.Med_Date, for_Name = erx.Name, for_Active = erx.Active, for_Resolved = erx.Resolved, for_Comments=erx.Comments });
return Json(new { Listemr_t_Immunization, JsonRequestBehavior.DenyGet });
} else
{
return Json(new { success = false });
}
} else
{
return Json(new { success = false });
}
}
另外,處理API類內容的成功和失敗的更好方法是使用HTTP錯誤代碼。
Response.StatusCode = (int)HttpStatusCode.Created;
return Json(new { results });
編輯
發送回您的請求列表。
這行需要更改:
List<emr_t_Immunization> Listemr_t_Immunization = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList();
我猜這是db是您的上下文對象,如果是這樣,請替換為:
var results = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList()
我不是100%知道erx是什么,但是如果您檢查是否有結果,則將其更改為。
if (results.Any())
{
Response.StatusCode = (int) HttpStatusCode.Created;
return Json(new { results });
} else
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Failed");
}
因此,所有代碼都將顯示為:
[HttpPost]
public JsonResult getmedi(int patient2)
{
if (patient2.ToString() != "")
{
string roster = objOrgCont.getmedi(patient2);
JavaScriptSerializer ser = new JavaScriptSerializer();
emr_t_Immunization erx =(emr_t_Immunization)ser.Deserialize(roster, typeof(emr_t_Immunization));
var results = db.emr_t_Immunization.Where(Allemr_t_Immunization => Allemr_t_Immunization.Patient_Id == patient2).ToList()
/// You can make that query easier to read by having
/// var results = db.emr_t_Immunization.Where(a => a.Patient_Id == patient2).ToList()
if (results.Any())
{
Response.StatusCode = (int) HttpStatusCode.Created;
return Json(new { results });
} else
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Failed");
}
} else
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json("Failed");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.