Jsonresult values are received by javascript but fields do not receive value

Asked

Viewed 31 times

0

Javascript function receives values but fields are not receiving values.

Function of the Controller

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>
  • The append is used to insert content at the end of elements. It is correct that you use the val to add values.

  • Another thing, your Edit for has no ID. You can modify it to: @Html.TextBoxFor(x => x.SequenciaInicial, new { id = "SequenciaInicial") })

2 answers

0

Good morning Cyberiacs, to serialize json in my Rest applications I use Newtonsoftjson - https://www.newtonsoft.com/json

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 just on your controllers, return the object of the seginte form

Return Ok(seuObjecto);

your object should be c#, so the newtonsoft plugin will serialize js for you.

I hope I’ve helped!

0

Change the code:

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

for:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.