【问题标题】:Not Returning To AJAX Success From C# Controller Action没有从 C# 控制器操作返回 AJAX 成功
【发布时间】:2016-03-31 04:47:59
【问题描述】:

不从 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'

【问题讨论】:

  • 你的意思是每次出现错误回调??
  • 我要返回列表Listemr_t_Immunization
  • return Json(new { Listemr_t_Immunization, JsonRequestBehavior.AllowGet });试试看
  • 您现在的实际问题是什么?更新您的原始帖子以更了解您想要做的事情,我会尽力帮助您
  • 好的,我会更新,但我想返回列表 Listemr_t_Immunization 以取得成功 data AJAX 调用我应该怎么做

标签: c# jquery ajax


【解决方案1】:

乍一看,您似乎可以将您的失败返回移动到 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");
      }

    }

【讨论】:

  • 我想返回列表 Listemr_t_Immunization 到我的 AJAX 调用成功数据我应该怎么做
猜你喜欢
  • 1970-01-01
  • 2014-12-23
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
相关资源
最近更新 更多