Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to pass a random string from my Controller to the View.

Here is my Controller code:

 [HttpPost]
    public ActionResult DisplayForm(UserView user)
    {
       //some  data processing over here
        ViewData["choice"] = "Apple";

        return RedirectToAction("Next", "Account");
    }

Now I want to pass that data value "Apple" to my view Next.cshtml which is created as follows:

//View: Next.cshtml

  @{
    ViewBag.Title = "Thanks for registering";
    Layout = "~/Content/orangeflower/_layout.cshtml";
   }
    <p>Your favorite fruit is:</p>@ViewData["choice"]

But I am not able to see my data in the browser when the project runs.

Here is the snapshot:

1) On debug, the controller showing the value:

enter image description here

2) The browser view doesn't show the value "Apple"

enter image description here

3) On further debug to my Next.cshtml View: enter image description here

Why is the value not getting passed to the View correctly. Both my controllers for Next and DisplayForm are within the same Controller AccountController.cs , still value not getting displayed.

Can someone help me solve this ?

share|improve this question

2 Answers

up vote 6 down vote accepted

You are not rendering a view, you are redirecting. If you wanted to pass some information tyo the view you need to return this view after adding it to ViewData:

[HttpPost]
public ActionResult DisplayForm(UserView user)
{
    //some  data processing over here
    ViewData["choice"] = "Apple";

    return View();
}

If you want to pass a message that will survive after a redirect you could use TempData instead of ViewData.

[HttpPost]
public ActionResult DisplayForm(UserView user)
{
    //some  data processing over here
    TempData["choice"] = "Apple";

    return RedirectToAction("Next", "Account");
}

then inside the Next action you could fetch the data from TempData and store it inside ViewData so that the view can read it.

share|improve this answer
ohh so I need to write return View("Next","Account") as per my code right if I am not redirecting ? – Parth Doshi Jan 10 '12 at 12:59
@ParthDoshi, yes, if you want to use ViewData directly. But if you want to keep the Redirect-After-Post pattern you could use TempData as shown in my answer. – Darin Dimitrov Jan 10 '12 at 13:01
thanks it worked !! :) – Parth Doshi Jan 10 '12 at 13:02
@ParthDoshi, great, glad I could help. – Darin Dimitrov Jan 10 '12 at 13:06

You are performing a post - redirect - get. The ViewData is being set for this request, which returns a redirect, clearing the ViewData, then another request happens which does not have the data. Use TempData instead and it will be added to the ViewData automatically on the next request.

share|improve this answer
1  
thanks! it was very helpful indeed. – Parth Doshi Jan 10 '12 at 13:04
Very good question layout and explanation. – NickLarsen Jan 10 '12 at 13:07
Thanks! Sometimes I feel StackOverflow should allow us to accept two answers, one primary and the other as a secondary answer. Although, I got an answer from another user, I felt your answer was also good enough for me to understand the mistake in my code, so thanks for the quick reply – Parth Doshi Jan 10 '12 at 13:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.