RedirectToAction() destroys string data - c#

Inside my controller I have string variable
private string notificationMessage = "";
which I want to use to copy it's content to ViewBag.Message and display that content on the view.
So inside my edit action I populate it's (notificationMessage) content like this
notificationMessage = "data is succ. updated!";
return RedirectToAction();
But after redirection to Index action this string variable is empty;
How can solve this?

Use TempData instead of ViewBag. It persists between requests.

It is because RedirectToAction returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action. Since HTTP is stateless, you can't simply set something in one action and get it in another(when it is another GET request).
What you can do is
1) pass a querystring to your new action and check that in the next action method and show a message according to that.
return RedirectToAction("ThankYou","Account",new {msg="success"});
and in your ThankYou action
public ActionResult ThankYou(string msg)
{
var vm=YourSuccessViewModel();
if(msg="success") // you may do a null checking before accessing this value
{
vm.Message="Saved Successfully";
}
return View(vm);
}
2) Store in a temporary place like Session / TempData. TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only!
TempData["UserMsg"] = "Saved Successfully";
return RedirectToAction("ThankYou","Account");
and in your ThankYou action you can read it and show to user as needed.
public ActionResult ThankYou(string msg)
{
var msg = TempData["UserMsg"] as string;
//to do : do what you want with the message and return the view.
}
Session object is the backing store for the TempData object, and it is destroyed more quickly than a regular session, i.e., immediately after the subsequent request.

Related

Using TempData with Post Redirect Get pattern

There are a number of folks that advocate using TempData with the PRG pattern in .NET Core 2.2.x.
From what I understand, this line of code stores data:
TempData["foo"] = JsonConvert.SerializeObject(model);
And the following reconstitutes the model and then deletes it from the TempData construct:
string s = (string)TempData["Model"];
var model = JsonConvert.DeserializeObject<ModelType>(s);
So given this transient nature of TempData, imagine the following PRG construct. The user POSTs to the UserInfo action, which packages the model into TempData and redirects to a UserInfo GET. The GET UserInfo reconstitutes the model and displays the view.
[HttpPost]
public IActionResult UserInfo(DataCollectionModel model) {
TempData["Model"] = JsonConvert.SerializeObject(model);
return RedirectToAction("UserInfo");
}
[HttpGet]
public IActionResult UserInfo() {
string s = (string)TempData["Model"];
DataCollectionModel model = JsonConvert.DeserializeObject<DataCollectionModel>(s);
return View(model);
}
The user is now on the /Controller/UserInfo page. If the user pressed F5 to refresh the page, the TempData["Model"] would no longer be there and the GET on UserInfo would fail. The fix might be to store the model in TempData after reading it, but wouldn't that result in leaking memory?
Am I missing something?
TempData can be used for storing transient data . It is useful for redirection, when data is needed for more than a single request.When an object in a TempDataDictionary is read, it will be marked for deletion at the end of that request.
That means if you put something on TempData like
TempData["value"] = "someValueForNextRequest";
And on another request you access it, the value will be there but as soon as you read it, the value will be marked for deletion:
//second request, read value and is marked for deletion
object value = TempData["value"];
//third request, value is not there as it was deleted at the end of the second request
TempData["value"] == null
The Peek and Keep methods allow you to read the value without marking it for deletion. Say we get back to the first request where the value was saved to TempData.
With Peek you get the value without marking it for deletion with a single call, see msdn:
//second request, PEEK value so it is not deleted at the end of the request
object value = TempData.Peek("value");
//third request, read value and mark it for deletion
object value = TempData["value"];
With Keep you specify a key that was marked for deletion that you want to keep. Retrieving the object and later on saving it from deletion are 2 different calls. See msdn
//second request, get value marking it from deletion
object value = TempData["value"];
//later on decide to keep it
TempData.Keep("value");
//third request, read value and mark it for deletion
object value = TempData["value"];
You can use Peek when you always want to retain the value for another request. Use Keep when retaining the value depends on additional logic.
Make the following changes in your Get action
public IActionResult UserInfo()
{
//The First method ,
string s = (string)TempData.Peek("Model");
//The Second method
string s = (string)TempData["Model"];
TempData.Keep("Model");
if(s==null)
{
return View();
}
else
{
User model = JsonConvert.DeserializeObject<User>(s);
return View(model);
}
}

Pass created data to another controller

I am using ASP.NET MVC Entity Framework and I have a page to insert data
public ActionResult Create()
{
return View();
}
// POST: /Home/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="id,firstname,lastname,email,guests,guestfirstname,guestlastname,productInterest,occupancyTimeline,isInvestment,timeSlot,dateSlot")] CP_LC_Preview cp_lc_preview)
{
if (ModelState.IsValid)
{
db.Data.Add(cp_lc_preview);
db.SaveChanges();
return RedirectToAction("Confirm", new { info = cp_lc_preview });
}
return View(cp_lc_preview);
}
What I am trying to do is take that data that was just entered and pass it to another controller to display. like a confirmation page.
Here is my method for the confirm page
public ActionResult Confirm()
{
return View();
}
You may consider following the PRG pattern.
PRG stands for POST - REDIRECT - GET. With this approach,After you successfully save the data, you will issue a redirect response with a unique id in the querystring, using which the second GET action method can query the resource again and return something to the view.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="id,firstname,lastname,email,guests,guestfirstname,guestlastname,productInterest,occupancyTimeline,isInvestment,timeSlot,dateSlot")] CP_LC_Preview cp_lc_preview)
{
if (ModelState.IsValid)
{
db.Data.Add(cp_lc_preview);
db.SaveChanges();
var id = cp_lc_preview.Id;
return RedirectToAction("Confirm", new { id = id });
}
return View(cp_lc_preview);
}
and in your Confirm action method, have id parameter and using the value of that read the record from the db again and use as needed.
public ActionResult Confirm(int id)
{
var d = db.Data.FirstOrDefault(g=>g.Id==id);
// Use d as needed
// to do : Return something
}
TempData
If you do not prefer to have this id in the url, consider using TempData to pass the data. But TempData has a short life span. Once read, the data is gone. TempData uses Session behind the scene to store the data.
TempData["NewItem"] = cp_lc_preview;
return RedirectToAction("Confirm", "controllerName");
and in the Confirm method
public ActionResult actionname()
{
var model=TempData["NewItem"] as CP_LC_Preview
// to do : Return something
}
For your reference
How do I include a model with a RedirectToAction?
You can use TempData for that.
TempData["YourData"] = YourData;//persist data for next request
var myModel=TempData["YourData"] as YourData //consume it on the next request
What is TempData ?
TempData is meant to be a very short-lived instance, and you should only use it
during the current and the subsequent requests only.
Since TempData works this way, you need to know for sure what the next request will be, and
redirecting to another view is the only time you can guarantee this.
Therefore, the only scenario where using TempData will reliably work is when
you are redirecting.This is because a redirect kills the current request , then creates a
new request on the server to serve the redirected view.
Simply said, Asp.Net MVC TempData dictionary is used to share data between
controller actions.
The value of TempData persists until it is read or until the current user’s session times out.
By default, the TempData saves its content to the session state.
TempData values are marked for deletion when you read them. At the end of the request,
any marked values are deleted.
The benefit is that if you have a chain of multiple redirections it won’t cause TempData to
be emptied the values will still be there until you actually use them, then they clear up after
themselves automatically.

How to use sessions,Tempdata in asp.net mvc4

I am working on asp.net with mvc 4 architectute. Here i have two controller Display and SessionEx. In Display Controller i have an method like below
public ActionResult SessionExample()
{
TempData["FortheFullRequest"] = "FortheFullRequest";
string v = Session["Session1"].ToString();
ViewData["Myval"] = "ControllertoView";
ViewBag.MyVal = "ControllertoView";
Session["Testing1"] = "Testing Session";
return RedirectToAction("SomeOtherAction", "SessionEx");
}
In the SessionEx controller i have the method as below
public ActionResult SomeOtherAction()
{
string str1 = Convert.ToString(Session["Testing1"]);
string str2 = Convert.ToString(TempData["FortheFullRequest"]);
return View();
}
I am debugging the project and i have also used watch to look at the gets stoted in tempdata and session. In the start the appropriate value in both session and tempdata but when the cursor reaches at RedirectToAction method all values gets stored in session and tempdata becomes null.Please help me someone here.
If you want to store data that will be used after a redirect is made to another action method, then use Session.
TempData is mainly for one-time, short-lived, requests as discussed in this this SO question
You are likely finding that the data in TempData is not there after you have redirected to SomeOtherAction(), which is by desing with how TempData works.
To be honest, I never use TempData, I don't see the point myself.

TempData[] getting wiped despite no requests taking place

TempData is supposed to persist for a subsequent request. However when I check the TempData in a subsequent controller I get a null. Why is TempData[] getting wiped off. Here are my two controllers. I am calling them in succession. So TempData[] should persist from one request to the next. Here is code from controller 1:
[HttpGet]
public ActionResult EditCampaign(int? Id)
{
ViewBag.Banner = bannerText.Replace(" ", " ");
// This is passed in case we need to add a new product or edit one. Then we'll
// need the campaign id for reference purposes.
TempData["campaign_id"] = Id;
TempData["campaignBanner"] = bannerText.Replace(" ", " ");
ViewBag.StartDate = debugger.startDate.ToShortDateString();
ViewBag.EndDate = debugger.endDate.ToShortDateString();
int Id1 = Convert.ToInt32(TempData["campaign_id"]);
int convertedId1 = Id1
Here is where I try to get the value in Controller 2:
[HttpPost]
public ActionResult EditCampaign(CampaignDTO camps)
{
// Do this up top. i.e. get the campaign ID
camps.Id = Convert.ToInt32(TempData["campaign_id"]);
string success = "";
I suspect that assigning the value of TempData in the penultimate line of controller 1 might be wiping off the data. If thats the case that is something new and not documented. Please help.
Reading the data is enough to remove it. Here's the relevant source:
public object this[string key]
{
get
{
object value;
if (TryGetValue(key, out value))
{
_initialKeys.Remove(key);
return value;
}
return null;
}
set
{
_data[key] = value;
_initialKeys.Add(key);
}
}
So in controller 1, when you say:
int Id1 = Convert.ToInt32(TempData["campaign_id"]);
you are actually removing the data you just inserted.
There are many ways to work around this, but if you must read it from TempData, use Peek() instead.
You have some flawed info. The data stored in the TempData property persists for only one request.
http://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.tempdata%28v=vs.118%29.aspx
TempData is useful when you are redirecting (HTTP 302 or 303, i.e. RedirectToAction in MVC) to pass along some context. It does not persist across separate HTTP requests. Based on your code sample, it suggests your client code first makes a GET to controller one, then issues a second request to controller two.
Your best bet is probably to return the necessary data as part of your response in controller one, then make that part of the incoming request to controller. You could potentially also use Session variables, https://code.msdn.microsoft.com/How-to-create-and-access-447ada98.

ASP.NET MVC 3 - Detect Whether Current Page Was Redirected To in a Post Redirect Get Workflow

In my C# .NET 4 MVC 3 application I have a delete controller for a set of CRUD pages which uses the Post Redirect Get pattern to redirect to an Index controller after a successful delete. I would like to render a button on the Index page only if this page was NOT redirected to by such an action. Is there a simple way to detect if the current page was redirected to (i.e. was reached as the result of a PRG redirect)?
After reading http://blog.simonlovely.com/archive/2008/11/26/post-redirect-get-pattern-in-mvc.aspx my current approach is to set this in my delete controller with TempData after the DeleteMyEntity method has succeeded:
try {
MyService.DeleteMyEntity(MyViewModel.MyEntity);
TempData["Redirected"] = true;
args = new RouteValueDictionary(new { Foo = 1, Baa = 2 });
return RedirectToAction("Index", args);
} catch (Exception e)
{
//Logging etc. - redirect should never be reached on exception (and TempData item not set)
throw(e);
}
then in my Index controller I check to see if this value exists and is true:
if (TempData["Redirected"] != null)
{
//we can then do something useful with this
}
Another opportunity I see would be to add another item to args and check for this in the controller, but in this case I may as well just use TempData. Is there a way to do this using a HTTP Response code on the request without needing to pass this data through with TempData or a similar mechanism?
another route would be to set up a global actionfilter that "injects" that flag for you...
public class RedirectDetect: ActionFilterAttribute{
public override void OnActionExecuted(ActionExecutedContext filterContext){
if (filterContext.Result is RedirectToRouteResult ||
filterContext.Result is RedirectResult)
{
TempData["Redirected"] = true;
//or what ever other indicator you want to set
}
}
}
And then you can just call redirectToAction("Index") and then check in your receiving handler
sidenote: I challenge you to say RedirectDetect aloud and not smirk.
I use TempData in a similar fashion - for instance, to show a status message (after redirecting to) my view when a record has been added / updated / deleted. This is the kind of simple, throw-away stuff that TempData is used for, so I say what you have is appropriate.
Personally I wouldn't mess with HTTP status codes unless I had an absolute need for it. And you could probably do something with the referrer http header, but again, that would be much messier and more complicated than just using TempData. You have a clean, simple solution that works, I say go with what you have.
I am not aware of any simpler mechanism and have been using TempData for quite some time to implement Post-Redirect-Get features. As far as I know, this is specifically one of the reasons for TempData's existence. I would continue using it.
There is no way to tell the difference between requests as a result of a 3xx redirection or a straightforward user-initiated GET. The best way is to provide a querystring argument that is only appended by the redirection for the initial POST request, but there is nothing stopping the user from reloading the page with the same querystring.
Hmmm, or you could send a cookie with the redirection from the POST, then remove the cookie in the response of the subsequent GET, like so:
public ActionResult PostHandler(ViewModel model) {
SetCookie("redirected", "true"); // psuedocode
return Redirect("GetHandler2");
}
public ActionResult GetHandler2() {
if( GetCookie("redirected") == "true" ) {
// do something
}
DeleteCookie("redirected");
}
Building off of George's answer:
public class MarkRedirects : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext context)
{
if (context.Result is RedirectToActionResult ||
context.Result is RedirectResult)
{
// Obtain and verify the underlying IController
var controller = context.Controller as Controller;
if (controller != null)
controller.TempData["Redirected"] = true; // or set other dictionary data here
}
}
}
The conditional checks of context.Result can vary based on what method you used to redirect, for instance if you redirected the user via the RedirectToAction() method, context.Result is RedirectToActionResult will return true but context.Result is RedirectToRouteResult will not.
Because of this, you will want to change up that conditional based on your personal taste of how you redirect users. The current code would work for OP's situation.
If you're going to be using this everywhere, it may be wise to modify a base controller.

Categories

Resources