簡體   English   中英

調用AJAX(ASP.NET MVC)后無法顯示值

[英]Cannot show value after AJAX call (ASP.NET MVC)

我有兩個<p>字段,我需要在其中分配文本

這是html代碼:

 <p id="appId" style="visibility: hidden;"></p>
<p id="calculationId" style="visibility: hidden;"></p>

我這樣打AJAX電話

  $('#openCalculationConsumables').click(function() {
    addConsumables();
});

function addConsumables() {
    var patientName = $('#patientsId').val();
    var finding = $('#findingvalue').val();
    var procedure = $('#procedurevalue').val();
    var model = {
        findingValue: finding,
        procedureValue: procedure,
        patientId:patientName
    }
    $.ajax({
        url: '@Url.Action("AddIndividualCalculation", "Calculations")',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(model),
        type: 'POST',
        dataType: 'json',
        processData: false,
        success: function (data) {
            $('#masters_data').load('@Url.Action("IndividualCalculationConsumables", "Calculations")', function() {
                var list = data;
                $('#calculationId').text(list[0].calcid);
                $('#appId').text(list[0].appid);
            });

        }
    });
}

這是我的后端代碼:

  public JsonResult AddIndividualCalculation(string findingValue, string procedureValue,int patientId)
    {
        using (var ctx = new ApplicationDbContext())
        {
            Calculation calc = new Calculation
            {

            };
            ctx.Calculations.Add(calc);
            ctx.SaveChanges();
            int calculationId = calc.Id;
            Appointment app = new Appointment
            {
                Calculation_id = calculationId,
                FindingContent = findingValue,
                ProcedureContent = procedureValue,
                Patient_id = patientId

            };
            ctx.Appointments.Add(app);
            ctx.SaveChanges();
            int appointmentId = app.Id;
            var items = new
            {
                appid = appointmentId,
                calcid = calculationId
            };


            return Json(items,JsonRequestBehavior.AllowGet);
        }
    }

我設置了斷點,然后看到items有值。 在控制台日志中,我有這個{appid: 1006, calcid: 1006}

但是我無法將其分配給<p>並出現此錯誤。

無法讀取未定義的屬性“ calcid”

我的問題在哪里?

感謝幫助。

$('#masters_data').load('@Url.Action("IndividualCalculationConsumables", "Calculations")', function() {
    var list = data;
    $('#calculationId').text(list[0].calcid);
    $('#appId').text(list[0].appid);
});

list [0]未定義,因為您僅返回一個匿名對象而不是對象列表

new {
   appid = appointmentId,
   calcid = calculationId
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM