Roxy'Pro
Roxy'Pro

Reputation: 4454

Asp.net MVC passing data from one method to another method then to a view

I am confused by passing data from one ActionResult to another ActionResult method.

What this is about let me describe,

I need to pass value from one method to another, and that value must be available on view that I'm rendering from that another method.

What I saw in my project is this (this is Edit but [HttpPost] Edit, and it is redirecting to Edit also but with [HttpGet] insted of [HttpPost]):

TempData["Success"] = True;
return RedirectToAction("Edit/" + customer.Id, "Customer");

And what is done on [HttpGet] Edit :

 if (TempData["Success"] != null && TempData.ContainsKey("Success"))
 ViewBag.Success = Convert.ToString(TempData["Success"]);
 return View(model);

As you can see guys on [HttpPost] TempData["Success"] is set to True; And Redirection is made to [HttpGet] method and there is written next code:

if (TempData["Success"] != null && TempData.ContainsKey("Success"))
 ViewBag.Success = Convert.ToString(TempData["Success"]);
 return View(model);

So I am wondering why it's needed to set TempData and later based on value of that TempDate let's set value to a ViewBag, can't I just set value of ViewBag on my first ActionResult before redirection so it can be available also on a View even if view is being rendered/called from HttpGet action result?

For example:

instead of this:

TempData["Success"] = True;
return RedirectToAction("Edit/" + customer.Id, "Customer");

Can I simply write in my HttpPost

ViewBag.Success = True;
return RedirectToAction("Edit/" + customer.Id, "Customer");

Or this is needed to be done with TempData because ViewBag wouldn't be available on a View if I don't put value in it on a ActionResult which is redirecting to a View, and in this case it is HttpGet and not HttpPost (so that means I need to set ViewBag value on a HttpGet?)

And If I must do it this way, I could also use two ViewBags, and not ViewBag and TempData?

And why would someone solve it like this at all? Is this correct approach or what?

Upvotes: 1

Views: 4854

Answers (3)

Rey
Rey

Reputation: 4000

First ViewBag can not be used in this case because you are making a redirection. A redirection means making a full request at a specific action/controller. ViewBag essentially is used only to pass data from action to view in a single request which means making a redirecting will wipe any data saved in ViewBag. In this case the only best way I think is using TempData because it persist the data even if a redirection is made, but when the redirection is made within the same controller, so if you make a redirection to another controller you will eventually loose your data registered in your TempData object.

I can see you are trying to find a way to alert the user for different notifications but you can see my answer here in details for that approach:

Upvotes: 0

Win
Win

Reputation: 62301

can't I just set value of ViewBag on my first ActionResult before redirection so it can be available also on a View

You cannot, because Http is stateless. So, we use TempData to store temporary data in order to persist Http requests.

And why would someone solve it like this at all? Is this correct approach or what?

That approach you have is fine in term of ASP.NET MVC, since we have no other way to persist temporary data between Http requests.

If you see yourself doing it very often, you might want to consider using alert extension methods to display a message inside toaster.

enter image description here

Usage

[HttpPost]
public ActionResult Edit(SettingModel model)
{
   if (ModelState.IsValid)
   {
       ...
      return RedirectToAction("List")
          .WithSuccess($"Setting was updated successfully.");
   }
   return View(model);
}

Upvotes: 1

matrixresolved
matrixresolved

Reputation: 1

There are couple of techniques to implement the Post/Redirect/Get pattern.

TempData is one way of passing information for a single redirect. The drawback with this approach is that if the user hits refresh (F5) on the end redirected page he will no longer be able to extract the data as it will be gone from TempData for subsequent requests.

The other two methods are Query string parameters and Persistence.

Upvotes: 0

Related Questions