How to get data entered in a form not yet recorded?

0

Hello,

In an application using MVC , how do I get the data entered by the user on a form, and before writing to the database, I return a view with the data entered for the user and only after that conference, does it give the option to record or return to the form with the data? Is it through the use of js or some method in controller ?

If possible answer with some simple example, I thank you.

    
asked by anonymous 06.11.2015 / 13:13

2 answers

1

The didactic way of doing this is to create two Actions in the Controller , one of which only passes the data to the confirmation screen and the other actually makes the changes in bank:

[HttpPost]
public ActionResult Confirmacao(MeuModel meuModel) 
{
    if (ModelState.IsValid) 
    {
        return View(meuModel);
    }

    // Quando cai aqui, é porque a validação não passou.
    return View("ViewAnterior", meuModel);
}

[HttpPost]
public ActionResult Salvar(MeuModel meuModel, String botaoPressionado)
{
    if (ModelState.IsValid) 
    {
        if (botaoPressionado == "Editar") 
        {
            return View("ViewAnterior", meuModel);
        }

        /* Coloque aqui a lógica para salvar e redirecionar o usuário
           para a próxima tela. */
    }

    // Aqui cai novamente se alguma coisa não estiver válida.
    return View(meuModel);
}

This confirmation form should have all the hidden and two buttons: one to confirm:

<input type="submit" name="botaoPressionado" value="Confirmar" />

And another to edit the form again:

<input type="submit" name="botaoPressionado" value="Editar" />

Another way would be to display a modal with the filled data and put the confirm button inside the modal. It gives a little more work, but then there would not be two Actions for confirmation.

This last approach is newer and does not work well for legacy browsers.

    
06.11.2015 / 16:41
1

What you're wanting to do is nothing more than an extra page. So when you send the data from the form to the server, it should be routed to a controller that will do everything that is needed and call the conference view . This may send the confirmation, which may be another controller that will write through the model , and will probably emit another view . Although you can do a pre-validation on the previous page, only after it is actually confirmed is the final validation done.

The basic technique would be to send the data both on the home page and on the final page when there is confirmation. But it could do something more advanced and hold the data in session on the server side and not need to send the second time. Obviously in this case, you can only confirm or cancel, you can not change any data.

There is no secret. Treat this page as if it were an ordinary page.

Eventually you can take advantage of the code and perhaps the same basic page for more than one operation. But if it's starting, take it easy, first get the goal, then think about reusing the code.

JavaScript can be a facilitator to give a better user experience, but it is not necessary.

You can think of other techniques to avoid this step being taken. I particularly do not like confirmation pages, neither as a user nor as a developer.

    
06.11.2015 / 13:49