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

Detect a jump in track bar
c0000005 Access_Violation calling Process.Start when running app in remote powershell session
asp.net 301 redirect for huge data
Error CS1729, What am I doing wrong?
How to update first column of a datatable for all rows using LINQQ or any other method?
I'm getting an xpath error when navigating to an element in the soap message
AffectsMeasure does not seem to work for custom wpf control
Making grid in WPF using loops dynamically
Read UID of Nfc tag even if there is no NDEF record Windows 10 UWP
Unidata File Manipulation with C#
How to receive H.264 stream and decoding in UWP app?
Error when adding a controller in Entity Framework
error : This project references NuGet package(s) that are missing on this computer
Generic Abstract class who inherits members from Passed Type Parameter
.Net Winforms notify all open windows
Metadata path in Connection String

Categories

HOME
http
oop
network-programming
windows-server-2008
file-upload
spss
multipartform-data
country-codes
obd-ii
box-api
olap
kairosdb
velocity
web-parts
windows-phone
hapijs
project-structure
descriptor
connection-pooling
suitescript
rollupjs
request-uri
ipython-parallel
schedule
overflow
foxpro
mplab
rtos
imageprocessor
android-geofence
aweber
azure-storage-files
anypoint-studio
libconfig
instances
jslider
keychain
mixed-models
superclass
libzip
ocpjp
opos
lucee
scanf
addressbook
portaudio
papyrus
symantec
patching
weblogic-maven-plugin
camellia
video-embedding
wordpress-plugin
apache2-module
skscene
bioconductor
lytro
adempiere
om
cyberduck
android-filterable
borland-c++
transparentproxy
system.diagnostics
tidesdk
ultraedit
chrome-mobile
qxorm
crystal-reports-7
api-eveonline
disparity-mapping
time-frequency
behance-api
rails-migrations
new-operator
connections
data-layers
nop
adabas
surfaceview
ie-developer-tools
oc4j
azure-pack
xrandr
android-library
magicsuggest
msbuild-task
spawn
architectural-patterns
brew
database-restore
screensharing
configurationsection
erlide
motorola-droid
datejs
discussion-board
vectorwise
peoplepicker
backlight
method-call
lwp
llblgen
mass-emails
table-valued-parameters
lts
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