Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Vanilson 28 posts 150 karma points
    Jun 14, 2016 @ 04:57
    Vanilson
    0

    Passing values from a controller to view

    Hi guys, I want to know how do I pass a value from controller to view, I´m trying to use ViewBag, but the value is not presented. I got the following method that creates a new node using a the values of a contact form:

        public ActionResult HandleFormPost(Contact contact)
        {
            ViewBag.message= "A sua sugestão foi enviada com sucesso";
            var message= Services.ContentService.CreateContent(contact.Name+ " - " + DateTime.Now, CurrentPage.Id, "ContactForm");
            message.SetValue("name", contact.Name);
            message.SetValue("email", contacto.Email);
            message.SetValue("message", contacto.Message);
            Services.ContentService.SaveAndPublishWithStatus(message);
           return RedirectToCurrentUmbracoPage();}
    

    And the I use the ViewBag in the page

    @ViewBag.message
    

    But I got no results, anyone knows how to solve this??

    Best Regards

  • Dennis Adolfi 1082 posts 6445 karma points MVP 5x c-trib
    Jun 14, 2016 @ 05:53
    Dennis Adolfi
    2

    Hi Vanilson. When you do a redirect the value of your ViewBag gets lost. Instead, use TempData.

    TempData["Message"]

    http://www.dotnet-tricks.com/Tutorial/mvc/9KHW190712-ViewData-vs-ViewBag-vs-TempData-vs-Session.html

    ViewBag: It’s life lies only during the current request.

    TempData: TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).

    Hope this was helpful!

    All the best / Dennis

  • Vanilson 28 posts 150 karma points
    Jun 14, 2016 @ 10:39
    Vanilson
    100

    Hi Dennis, thank you for the reply, it works.

    Best Regards

  • Dennis Adolfi 1082 posts 6445 karma points MVP 5x c-trib
    Jun 14, 2016 @ 11:16
    Dennis Adolfi
    1

    Awsome Vanilson! Glad I could help! Have a great day!! :)

Please Sign in or register to post replies

Write your reply to:

Draft