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

Asked

Viewed 94 times

0

Hello,

In an application using the MVC, how do I get the data typed by the user in a form, and before recording in the database, I return a view with the data entered for the user to check and only after this conference, he gives the option to record or return to the form with the data? It would be through the use of js or some method in the controller ?

If possible answer with some simple example, I thank.

  • 1

    What have you tried?

  • I haven’t tried anything yet because I’ve thought of several ways but I can’t come up with any conclusions, and I’m also new to that language...

  • @Matheussilva suggest the read here. http://www.macoratti.net/13/04/mvc4_app.htm I started there and found very self-explanatory.

  • @Marconi Opa, thanks man! Despite being a beginner, I already have a good sense of the language (+/- 2 months)

  • You want the form to reappear completed or you want the data to appear as plain text, without the fields of a form?

  • @Romaniomorrisonmendez As simple texts only, and the possibility for the user to return to the form if you want to edit...

  • You want something that does more or less what Statement.RETURN_GENERATED_KEYS does in java, that’s it?

  • @Luangabriel Cara, I know almost nothing about java rs What I want you to do is "hold" the data entered in a form, and before writing to the database, show the data in html tags like <p>, <span>, etc... and only after showing, user confirms and writes to the database

Show 3 more comments

2 answers

1

What you’re wanting to do is nothing more than an extra page. So when you send the form data to the server, it should be routed to a controller who will do all that is necessary and call to view conference. The conference may send the confirmation, which may be another controller that will record through the model, and will probably send another view. Although you can do a pre-validation on the previous page, only after it is really confirmed is that the final validation should be done.

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

It has no secret, treat this page as if it were a common page.

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

Javascript may be a facilitator to give better user experience, but it is not necessary.

You can think of other techniques to prevent this step from being taken. I don’t particularly like confirmation pages, either as a user or as a developer.

  • So the intention is to have a "before" page of the data recorded in the bank. The form is a bit extensive (a kind of resume), ai it was decided to have this confirmation screen for the user to view more easily how the data was filled and if there is something wrong the intention is to change right there...

1


The didactic way to do this is to create two Actions in the Controller, where one only passes the data to the confirmation screen and the other effectively carries out the database changes:

[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 must have all fields hidden and two buttons: one to confirm:

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

And another to re-edit the form:

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

Another way would be to display a modal with the data filled and place the confirmation button inside the modal. It takes a little more work, but then it wouldn’t take two Actions for confirmation.

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

  • I did not understand very well the part that the fields in the confirmation page should be Hidden. The intention is to show the contents of these fields in <p>,<span>,<H3>...

  • So, because you will have to submit the form once more to confirm. To get the confirmed data back to the View, you will have to make one more form. It’s not nice to do this, but it solves your problem.

  • I understood, so I would put in this confirmation view, all fields with the property Hidden, and also the tags with the contents I want to show ?

  • Exactly. Like I said, it doesn’t look pretty, but it works.

Browser other questions tagged

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