Preview the data before saving to the database

Asked

Viewed 138 times

1

I’m doing a news site on ASP.NET MVC 5 and the need arose to see the Preview of the news before saving it in the bank.

With a action by name Preview I can send the data to her and send it to a View with the page design where the news will actually be shown, but lose the screen Create.

How to make it open a new Tab with the Preview and the screen of Create remained intact?

1 answer

3


Here’s what I’d do:

  • One Action calling for Preview with the following signature:

    public ActionResult Preview(Noticia noticia) { ... }
    
  • To View would have all the attributes Hidden:

    @Html.HiddenFor(model => model.Titulo)
    @Html.HiddenFor(model => model.ConteudoNoticia)
    
  • It would also have the Model properties written, as forecast:

    <div>@Model.Titulo</div>
    <article>@Model.ConteudoNoticia</article>
    
  • Then just send via POST the Model again for another Action. Could be the Create even:

    public ActionResult Create(Noticia noticia) { ... }
    
  • I tried to do it this way, but two Actions with the MVC subscription does not allow. public Actionresult Create(Noticia noticia) {} [Httppost] public Actionresult Create(Noticia noticia) {}

  • So, but note that I didn’t use the same name. A Action is called Preview (by the way, she has [HttpPost] also) and the other is the Create (also with [HttpPost]).

  • 1

    And even when building your form, you change the target form to _blank before you submit that then you will see on another page and the current

  • @gypsy-Morrison-Mendez I have Action Create by default, so I send this data to Action Preview right? Then send again to Action Create, but it does not let me receive with the News object because Create Httppost also receives this object and has the same name. At least here you’re making a mistake.

  • @Hermesautran There it is: as I said in the answer, you have to put the fields in hidden and within a <form> (preferably using @using (Html.BeginForm("Create", "Noticias"))) so you can make a POST with the data.

Browser other questions tagged

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