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

Asked

Viewed 772 times

1

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

  • I don’t know if the question is very clear, but you can use Jquery to upload additional partial views as the user wants to register more addresses, and post this as a collection on model.

  • you want to have a button that sends the values of the address related fields to the controller and save in the database ?

  • Man, from what I understand, the form is one, it doesn’t need another. You must have a button that persists the address in the bank, passing as FK the customer ID. By persisting, it saves and clears the form. This upload I would do by jquery, for the controller and there I would call the model that would make the persistence.

  • @Rsinohara I can have jquery add another partial address view, and then I’ll have two address collections sent to controller?

  • @Thiagofalcão I want to know if this is a "good" way to do...

  • @pnet yes, but this button, need to send only the address data, not the whole form, and it can not clear the form, because the person needs to continue with the registration of the person, the address is only a part...

  • Yes, a type like this: In jquery/ajax you would send this. I will reply as it would be done.

  • The registration of the person is being carried out... therefore, if I understand correctly, the model would need to contain a collection of addresses.

Show 3 more comments

2 answers

2


  • I have doubts about how to use Begincollectionitem, from what I understand, it creates a collection of the object I want to insert several times, as if it were some kind of vector? and then I need to have a button that goes doing this?

  • @This is right. Normally the creation of a new element from the list (not vector) is made from View to the Controller through an Ajax request.

  • When I put viewModel address as Ilist: Ilist<Addressmodel> in my person viewModel, I can no longer call its properties in partial view.

  • So. Don’t send EnderecoViewModel for View. Send another Viewmodel who has a IList<EnderecoViewModel> inside her. Got it?

  • I think that’s what I was doing, I put IList<EnderecoViewModel> inside pessoaViewModel Then I call the property in view so: model.enderecoViewModel.propriedade but then he says "Cannot resolve Symbol property".

  • Right. That’s because IList does not have a field called propriedade. If you want propriedade of a EnderecoViewModel, needs to iterate EnderecoViewModel using something like a foreach, for example.

  • Looking at your examples, in the other answers, there’s a if before the foreach that checks if viewModel is null, to then do the foreach, but mine is always coming null, then it does not render the view.

  • @Aesir Now it is the case that you open a new question by putting this code for us to analyze.

Show 3 more comments

0

In jquery/ajax you 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. This way you send the data that is in the fields of your form. In the controller, then you would have your persist method.

[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)
                {}
            }
        }

That would be about it. Adapt and see if it works.

  • AP said it wanted to register several addresses during the registration of people... this solution sends several addresses, but to associate with the user would be more complicated.

  • It can pass the user ID as key and there in the method, write the address by the past user ID. I see no problem in that. When I used Delphi, we called it Master/Detail.

  • Right... the problem is that as the view registers the user, there is no ID yet.

  • @Rsinohara, depends on how you do it. I can very well get the ID at the time of registration, because he wants to register more than one address, in a single form. So, it should be a cascade operation, first one and then the others. At the moment the BD generates the user ID, I take this ID and work with it.

Browser other questions tagged

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