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

-1

When I trigger my action address, it gets codigoCliente sends by parameter master/CadastrarEndereco?codigoCliente=1011 .

How can the chosen client address persist?

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);    
        }
    
asked by anonymous 19.11.2014 / 16:29

1 answer

0

You probably have a Model with the fields in your account, for example CadastroViewModel:

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

In your Controller , action 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 CustomerCode (or force the user to fill out before clicking the link), eg

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

In your View , you can use an ActionLink , passing the CustomerCode as a parameter and directing it to View > address registration like this:

@model CadastroViewModel
...
@Html.ActionLink("Cadastrar Endereço", "CadastrarEndereco", "NomeDoSeuController", new 
{
    codigoCliente = model.codigoCliente
})
    
19.11.2014 / 19:09