TextBoxFor returned Null Post

4

I have a form in which I make a query to an API using jquery. The query returns the data and populates the textboxfor with this data:

 $.getJSON("//viacep.com.br/ws/" + cep + "/json/?callback=?", function (dados) {

                if (!("erro" in dados)) {
                    //Atualiza os campos com os valores da consulta.

                    $("#Logradouro").val(dados.logradouro);
                    $("#Bairro").val(dados.bairro);
                    $("#Cidade").val(dados.localidade);
                    $('#Estado option[value="' + dados.uf + '"]').attr({ selected: "selected" });

                }

<div class="form-group col-md-2">
                        @Html.LabelFor(m => m.Cidade)
                        @Html.TextBoxFor( m => m.Cidade, new { @class = "form-control rounded", @placeholder = "Cidade do Condutor" })
                        @Html.ValidationMessageFor(m => m.Cidade, "", new { @class = "text-danger" })
                    </div>

 [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(CondutorViewModel condutor)
        {

           
            if (!ModelState.IsValid)
            {
                return View(condutor);
            }

            var result = CondutorRepositoryUI.GetInstance().AddOrUpdate(condutor);

            TempData["AtualizacaoCondutor"] = result.Mensagem;

            return RedirectToAction("Index");
        }

When I do the POST to the form the fields that were filled by jquery return null, although they appear on the screen the other fields that were typed appear normal.

    
asked by anonymous 16.06.2016 / 22:40

1 answer

1

What I imagine happened in your case.

Starting with the 2015 version of Visual Studio when using intellisense to generate a property in ViewModel , VS is generating as internal set , I've already looked for ways to avoid VS generating the property set as internal , but I did not get any satisfactory answers, the solution found by me is always to look in all solution, using Ctrl + Shift + F, by% with% and replace with% with%.

In image 1 we show how it gets with internal set

publicclassCondutorViewModel{publicstringCep{get;internalset;}publicstringCidade{get;internalset;}publicstringLogradouro{get;internalset;}publicstringEstado{get;internalset;}publicstringBairro{get;internalset;}}

Infigure2Ishowhowitgetswithpublicset

How is the Model

public class CondutorViewModel
    {
        public string Cep { get; set; }
        public string Cidade { get; set; }
        public string Logradouro { get; set; }
        public string Estado { get; set; }
        public string Bairro { get; set; }    
    }
    
28.07.2016 / 13:44