简体   繁体   English

调用AJAX(ASP.NET MVC)后无法显示值

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

I have two <p> fields where I need to assign text 我有两个<p>字段,我需要在其中分配文本

Here is html code: 这是html代码:

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

I make AJAX call like this 我这样打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);
            });

        }
    });
}

And here is my back end code: 这是我的后端代码:

  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);
        }
    }

I set breakpoint and see , that I have values in items . 我设置了断点,然后看到items有值。 In console log I have this {appid: 1006, calcid: 1006} 在控制台日志中,我有这个{appid: 1006, calcid: 1006}

But I cant assign it to <p> and have this error. 但是我无法将其分配给<p>并出现此错误。

Cannot read property 'calcid' of undefined 无法读取未定义的属性“ calcid”

Where is my problem? 我的问题在哪里?

Thank's for help. 感谢帮助。

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

list[0] is not defined as you are returning just an anonymous object not a list of objects list [0]未定义,因为您仅返回一个匿名对象而不是对象列表

new {
   appid = appointmentId,
   calcid = calculationId
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM