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

Task reports IsCompleted but still blocks on Result?
Non Transactional Execution Within a TransactionScope
Listbox item in pivot control doesn't get all available space
How to compare the byte array and byte array list?
Potential cause of inifinite loop, when binding to Property from SelectParameter of ObjectDataSource?
How to find location that visual studio is looking for an assembly reference
How to add a more generic event handler to an event at runtime
highlighting text according to output of a text to speech. in Asp.net c#
Refresh and Select Parent Form's Databound Combobox after Edits of Child Form
How to notify the views when the database changes? (MVC, C#)
Seemingly Innocent .NET Regular Expression Blows Up Page Response Time 10X
XNA C# content.load<spritefont> Object reference not set to an instance of an object
Overriding EditorFor ID field within nested EditorFor
Can a windows service be used on a Silverlight website?
List object properties that have an attribute applied
WPF C# - XML Edit Binding in Treeview

Categories

HOME
c#
drupal
max
excel-formula
model-view-controller
optimization
oracle-sqldeveloper
process
ldap
kohana
hibernate-mapping
restheart
revit-api
ephesoft
phpexcel
apache-httpclient-4.x
kairosdb
suitecrm
watson-dialog
caldav
openoffice-basic
tcpdf
apdu
inspec
expressionengine
pydub
jql
mplab
urhosharp
availability
kofax
socketcluster
electron-packager
quartz
bzip2
mtm
inner-join
logan-square
synchronized
spring-ide
activeandroid
opticalflow
blackberry
robotc
suitesparse
blitz3d
zsh-completion
alfred
angularfire
wordpress-plugin
dday
wonderware
gulp-cdnizer
redquerybuilder
borland-c++
polarion
p4python
grails-3.0.9
esri-arc-engine
shunting-yard
xts
disabled-control
pthreads-win32
junction-table
magicsuggest
spawn
boost.build
dnsbl
correctness
mail-gem
big-endian
bash4
nude.js
reflexil
mysql-connector
pinchzoom
object-tag
zipstream
norton
lgpl
xcdatamodel
arraycollection
table-valued-parameters
chromebug
miniport
linq-to-mysql
webdb
morfik
projectgen
wtsapi32

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