Textboxfor returned Null Post

Asked

Viewed 100 times

4

I have a form where I query an API using jquery. The query returns the data and fills 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 for the form the fields that were filled by jquery return null, although appear on the screen the other fields that were typed appear normal.

  • You can edit your answer by putting how your Controller is?

  • I did the editing by putting the controller.

  • What is the scope of <form/>? Using browser development tools, the one being sent to your controller from the browser?

  • Putting a breakpoint about var result = CondutorRepositoryUI.GetInstance().AddOrUpdate(condutor);, how the properties of condutor?

  • All other fields return the value normally, but the fields filled by jquery return null.

1 answer

1

What I imagine happened in your case.

From the 2015 version of Visual Studio when using intellisense to generate a property in the ViewModel, VS is generating as internal set, I have already looked for ways to avoid VS generating the set of the property as internal, but I did not get any satisfactory answer, the solution found by me is to always search in every solution, using Ctrl+Shift+F, by internal set and replace with public set.

In image 1 I show how it looks with internal set inserir a descrição da imagem aqui

public class CondutorViewModel
    {
        public string Cep { get; internal set; }
        public string Cidade { get; internal set; }
        public string Logradouro { get; internal set; }
        public string Estado { get; internal set; }
        public string Bairro { get; internal set; }
    }

In image 2 I show how it looks with public set inserir a descrição da imagem aqui

How does Model look

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

Browser other questions tagged

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