Register only some data of a form C # Asp.net MVC 5

1

I have a people registration form, where I have already registered her address, there was a need to be able to register more than one address for the same person at the time I am registering her, but I can not have a nested form inside one that only takes the address data, so what's the best way to do it? I want to have a button, which only takes the data that was typed in the address fields and register in the database, can I do that with a JQuery that would send the data to my people controller? I do not want to do something like the pop-up that appears to register the address, and I also do not want it redirected to another view, I wanted to take advantage of the same people registration view. I'm using partial views, and using different viewModels.

    
asked by anonymous 11.08.2015 / 16:19

2 answers

2

Nested forms do not, but you can have a View that expands the form according to your need for addresses.

This can be done through the BeginCollectionItem package.

I have already written several answers about using this package . If your question is more specific, you can ask another question.

    
11.08.2015 / 17:25
0

In jquery / ajax it would have this:

$.ajax({
        url: '/Endereco/GravaEndereco',
        datatype: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'POST',
        data: JSON.stringify({ _logradouro: $('#txtLogradouro').val(),
        _bairro: $('#txtBairro').val(),}),
        success: function (data) {

            alert('Registro gravado com sucesso!');

            window.location.href = '/Endereco/Endereco';
        },
        error: function (error) {
        }
    })

This would be in your jquery. That way you send the data that is in the fields of your form. In the controller, then you would have your method persist.

[HttpPost]
        public void GravaResponsavel(string _Logradouro, string _bairro)
        { 
            using(MeuEntities db = new MeuEntities())
            {    
                try
                {
                    resp.NM_Logradouro = _Logradouro;
                    resp.NM_Bairro = _bairro;

                    db.SaveChanges();
                }
                catch(Exception ex)
                {}
            }
        }

It would be more or less that. Adapt and see if it works.

    
11.08.2015 / 16:48