JsonResult values are received by javascript but fields are not given value

0

javascript function receives values but fields are not receiving values.

Controller function

public JsonResult GetSequencia(DateTime dataAbate)
        {
            db.Configuration.ProxyCreationEnabled = false;
            var lote = db.Lotes.Where(p => p.DataAbate == dataAbate).ToList();

            if (lote.Count <= 0)
            {
                var loteNull = db.Lotes.ToList();

                loteNull.Add(new Lote
                {
                    SequenciaInicial = 123,
                    SequenciaFinal = 456
                });

                return Json(loteNull);
            }

            return Json(lote);
        }

Javascript function within SECTION SCRIPTS

    @section Scripts {


        @Scripts.Render("~/bundles/jqueryval")
            $(document).ready(function () {
                $("#DataAbate").change(function () {
                    $("#SequenciaInicial").empty();
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("GetSequencia")',
                        dataType: 'json',
                        data: { dataAbate: $("#DataAbate").val() },
                        success: function (data) {
                            $.each(data, function (i, data) {

                                $("#SequenciaInicial").append(data.SequenciaInicial);
                                $("#SequenciaFinal").append(data.SequenciaFinal);

                            });
                        },
                        error: function (ex) {
                            alert('Falha ao buscar Proprietario.' + ex);
                        }
                    });
                    return false;
                })            
            });




}

Fields that are not receiving values

<div class="form-group">
    @Html.LabelFor(model => model.SequenciaInicial, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.SequenciaInicial, new { htmlAttributes = new { @class = "form-control", disabled = "disabled" } })
        @Html.ValidationMessageFor(model => model.SequenciaInicial, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.SequenciaFinal, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.SequenciaFinal, new { htmlAttributes = new { @class = "form-control", disabled = "disabled" } })
        @Html.ValidationMessageFor(model => model.SequenciaFinal, "", new { @class = "text-danger" })
    </div>
</div>
    
asked by anonymous 06.03.2018 / 01:02

2 answers

0

Good morning Cyberiacs, to serialize json in my applications Rest I use NewtonsoftJson - link

A configuration has to be made in Startup.js in the Configuration method including the following line.

var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>
().First();
jsonFormatter.SerializerSettings.ContractResolver = new 
CamelCasePropertyNamesContractResolver();

After that, it's only in your controllers, to return the object of the second form

Return Ok(seuObjecto);

yourObject must be the c # object, so the newtonsoft plugin will serialize js for you.

I hope I have helped!

    
06.03.2018 / 14:04
0

Change the code:

$.each(data, function (i, data) {  
   $("#SequenciaInicial").append(data.SequenciaInicial); 
   $("#SequenciaFinal").append(data.SequenciaFinal);
});

To:

$(data).each(function (idx, elem) {  
   $("#SequenciaInicial").append(elem.SequenciaInicial); 
   $("#SequenciaFinal").append(elem.SequenciaFinal);
});

Basically the change is in the use of name other than the variable data in the function $.each .

    
05.06.2018 / 19:41