How to update only the model in ASP.NET MVC?

Asked

Viewed 549 times

3

I am using ASP.NET MVC 5 and EF6 in a project, and I have a class that makes a query on the postal site and returns the address from the zip code. Until then, all right, what I would like to do is to use onblur so that in the registration the user leaves the ZIP code field, all other fields (except the number) related to address are updated.

I use heavily typed View, but I couldn’t find a way to update just the model. Can anyone help me?

  • 1

    Could show an example of how your code is?

  • 1

    If I understand correctly, you want to update the model without having a new request. That’s not how it works. The view is used once to render HTML that is delivered to the client. After that, the view (Razor) is no longer used until there is another request. What you need is then to use AJAX to fill in the fields according to the post office search.

1 answer

3


Following @Rsinohara’s comment, the way is through Ajax at View. In a writing system, I use a set of methods implemented using jQuery:

function TrocarCidades(id, estadoId) {
    $("#CidadeId").attr('disabled', true);
    $("#EstadoId").attr('disabled', true);

    $.ajax({
        url: "/Estados/SelecionarCidades/" + estadoId,
        success: function (data) {
            if (id == null) {
                $("#BairroId").empty();
                $("#BairroId").append('<option value>Selecione uma Cidade...</option>');
            }
            $("#CidadeId").empty();
            $("#CidadeId").append('<option value>Selecione...</option>');
            $.each(data, function (index, element) {
                $("#CidadeId").append('<option value="' + element.CidadeId + '">' + element.Nome + '</option>');
            });

            $("#CidadeId").attr('disabled', false);
            $("#EstadoId").attr('disabled', false);
            $("#CidadeId").val(id);
        }
    });
}

function TrocarBairros(id, cidadeId) {
    $("#BairroId").attr('disabled', true);
    $("#CidadeId").attr('disabled', true);
    $("#EstadoId").attr('disabled', true);

    $.ajax({
        url: "/Bairros/SelecionarPorCidade/" + cidadeId,
        success: function (data) {
            $("#BairroId").empty();
            $("#BairroId").append('<option value>Selecione...</option>');
            $.each(data, function (index, element) {
                $("#BairroId").append('<option value="' + element.BairroId + '">' + element.Nome + '</option>');
            });

            $("#BairroId").attr('disabled', false);
            $("#CidadeId").attr('disabled', false);
            $("#EstadoId").attr('disabled', false);

            if (id != null) {
                $("#BairroId").val(id);
            }
        }
    });
}

$("#CEP").blur(function () {
    $.ajax({
        url: "/Logradouros/BuscarPorCep/" + $(this).val(),
        success: function (data) {
            $("#EstadoId").val(data.Bairro.Cidade.EstadoId);
            TrocarBairros(data.BairroId, data.Bairro.CidadeId);
            TrocarCidades(data.Bairro.CidadeId, data.Bairro.Cidade.EstadoId);
            $("#Endereco").val(data.Descricao);
        }
    });
});

$("#EstadoId").change(function () {
    TrocarCidades(null, $(this).val());
});

$("#CidadeId").change(function () {
    TrocarBairros(null, $(this).val());
});

This implementation takes into account that neighborhoods, cities and states are Dropdownlists. You can simplify this algorithm for your need. Ie:

$("#CEP").blur(function () {
    $.ajax({
        url: /* Url de serviço de busca de CEP */
        success: function (data) {
            $("#Estado").val(data.Estado);
            $("#Bairro").val(data.Bairro);
            $("#Cidade").val(data.Cidade);
            $("#Endereco").val(data.Endereco);
        }
    });
});

Browser other questions tagged

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