c#


How to pass tempdata in RedirectToAction in ASP.Net MVC


I need to pass one logout successful message in one of the views but I am not able to do so. Here is what I have.
Not Working Solution:
//LogController:
public ActionResult Logoff()
{
DoLogOff();
TempData["Message"] = "Success";
return RedirectToAction("Index", "Home");
}
// HomeController
public ActionResult Index()
{
return View();
}
Index CSHTML File:
#Html.Partial("../Home/DisplayPreview")
DisplayPreview CSHTML File:
#TempData["Message"]
Working Solution
public ActionResult Logoff()
{
DoLogOff();
return RedirectToAction("Index", "Home", new { message = "Logout Successful!" });
}
public ActionResult Index(string message)
{
if (!string.IsNullOrEmpty(message))
TempData["Message"] = message;
return View();
}
Index CSHTML File:
#TempData["Message"]
But I want something like my first solution.
See if this works:
public ActionResult Logoff()
{
DoLogOff();
ControllerContext.Controller.TempData["Message"] = "Success";
return RedirectToAction("Index", "Home");
}
In the controller;
public ActionResult Index()
{
ViewBag.Message = TempData["Message"];
return View();
}
public ActionResult Logoff()
{
DoLogOff();
TempData["Message"] = "Success";
return RedirectToAction("Index", "Home");
}
Then you can use it in view like;
#ViewBag.Message
Since you don't show what DoLogOff() does, my guess is that you are abandoning the session, which means any data stored in session (like TempData) is lost. A new session does not get generated until the next page refresh, so it doesn't work.
What you might try is simply passing a flag to your Index view that will show the logged off message if it's present. I would NOT use the string message, like you show in your "working" example, because this can be coopted by attackers to redirect people to malicious sites.

Related Links

How to join two tables with linq?
MySQL standalone database embedded in c# application
cannot convert type struct to struct in c# unmanaged
Check if local date time exist NODA TIME
How to set properties of custom EditingControl of a custom DataGridViewColumn
Receiving Input From RawInput Registered Devices In c#
Set Transparent Highlight in combobox selection?
IAuthenticationFilter v.s. subclassing AuthorizeAttribute, which one is the perferred way to custmoize authencation?
HttpClient upload file without multipart in .net
Unable to present the data in jqgrid ASP.NET MVC
How do I list out installed voices on my C# Windows Forms App?
Calculate a simple angle
c# get data from application (Hearthstone)
Removing extra letter sets in an inconsistent text file using Regex
passing multiple objects as parameters to mvc 6 action
Weird behaviour of c# compiler due caching delegate

Categories

HOME
youtube-api
model-view-controller
leaflet
jenkins-pipeline
opc-ua
collision-detection
virtualhost
google-api-oauth
rtc
software-collections
phpexcel
elastalert
jetbrains
logstash-configuration
github-flavored-markdown
react-select
bookmarklet
taxonomy
google-street-view
sonarlint
restructuredtext
xna
google-drive-android-api
prepros
pumping-lemma
continuous-fourier
wercker
oracle-nosql
http-proxy
tunnel
leaderboard
gstreamer-1.0
rythm
client-server
mockserver
irr
inner-join
infovis
perldoc
sharpdevelop
chain
huawei
stringbuilder
portaudio
gevent
firebase-polymer
symantec
foreground
cosign-api
queuing
usps
std
sqldataadapter
jquery-draggable
web-performance
webservices-client
transport
borland-c++
fileinputstream
stderr
genero
data-quality-services
cordova-3
uiblureffect
information-hiding
formbuilder
powershell-v1.0
starling-framework
lastinsertid
google-mirror-api
google-oauth-java-client
fade
octokit
infinity.js
select2-rails
junction-table
mysql2
nsautoreleasepool
microsoft-speech-platform
shapado
ios6-maps
winrt-async
sup
maven-eclipse-plugin
parallel-python
xpolog
overlays
nscharacterset
webdb
j++
scalable

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App