How do I get the value of the parameter sent by a view and persist in the database?

Asked

Viewed 167 times

-1

When I set mine action register address, she picks up the codigoCliente, parameter master/CadastrarEndereco?codigoCliente=1011.

How to persist the chosen customer’s address?

My actions:

        public ActionResult CadastrarEndereco(int codigoCliente)
        {
            var endereco = new Endereco();
            endereco.CodigoCliente = codigoCliente;

            return View();
        }

        [HttpPost]
        public ActionResult CadastrarEndereco(EnderecoViewModel enderecoVM)
        {                
            if (ModelState.IsValid)
            {
              var endereco = Extentions.MapearEndereco(enderecoVM);

              endereco.CodigoCliente = codigoCliente;    
              EnderecoRepositorio.Cadastrar(endereco);
              EnderecoRepositorio.Commit();                                                                                      
            }

            return View(enderecoVM);    
        }
  • Why don’t you return an Addressmodel type instead of Addressmodel to your view? Then leave the Customer’s code in an Hidden input, when you see in the Post, you will have the Customer’s code

  • How does your business work? Do you have a customer registration and each customer has an address (which is more common to happen)? Or do you have an Address Register? Is your question about taking the parameter or how to persist in the bank? Anyway, give us more information about the functionality you are developing to try to help.

  • beauty! I have a customer register where I register and populate a grid ,,on my grid I have customer data and an address field,in the address field I have a link register address, next to each customer inserted,the link directs to a address registration view,passing through parameters the Codigodocliente in which, I want to insert an address. Problem is: I cannot persist my data in the database ,just because my view is not getting this value from the Code to enter into the database

  • You can put in your question the statement of Viewmodel used in the Controller?

1 answer

0

You probably have a Model with the fields of your register, for example:

public class CadastroViewModel
{
    //Propriedades para cadastro do cliente....
    public int CodigoCliente { get; set; }
}

In his Controller, one action Get will be responsible for displaying the screen/View with the form for the user to enter the data. In this action you need to fill in the code (or force the user to fill in before clicking on the link), example:

public ActionResult Cadastrar()
{
    var codigoClienteGerado = GerarCodigoCliente();
    var cadastroViewModel = new CadastroViewModel() { 
                                       CodigoCliente = codigoClienteGerado 
                                   };
    return View(cadastroViewModel);
}

In his View, you can use a Actionlink, passing the code as parameter and directing to the View registration of the address so:

@model CadastroViewModel
...
@Html.ActionLink("Cadastrar Endereço", "CadastrarEndereco", "NomeDoSeuController", new 
{
    codigoCliente = model.codigoCliente
})
  • @Hans Miller the answer was helpful?

Browser other questions tagged

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