tags:

views:

141

answers:

3

i have a dropdown list which select a value

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Screenname(FormCollection collection)
    {
        Viewdata["screenname"] = collection[0];

        return RedirectToAction("Index", new { ScreenName = ViewData["screenname"] });
    }

then i want to access this ViewData in other actions like this

   [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection, string screenname)
    {
        try
        {
            /// thats my dataobject which creates 

            DataObj.SaveData(Guid.Empty, collection, screenname);

            return RedirectToAction("Index", new { ScreenName = ViewData["screenname"] });
        }
        catch
        {
            return View("Error");
        }
    }

where index looks like this ...

    public ActionResult Index(string ScreenName)
    {
        ///thats my list 
        GetTable = new GetDataTable(ScreenName);

        return View(GetTable);
    }

First when i select the value and index gets executed properly.... but when i try to access the viewdata again it doesn't contain the value so anybody if please can help ... or alternate method to save and retrieve data .

+1  A: 

This has actually been covered quite often here. The solution for now is to use TempData to save the data you need before you use RedirectToAction().

If you do a search for "RedirectToAction" you'll find a number of posts covering this topic, such as this one.

The next official release of the framework will fix this.

womp
Even When i use tempdata i am not able to access the data . it still remains null . i have already tried that ...any other options
lucky
plus i have few more actions i want to pass the data to so please anyone help
lucky
+2  A: 

The ViewData object is specific for the particular action that is executing. To pass data between actions, use TempData. more on the difference between the two on MSDN.

You can also directly write to the session state through the Controller.Session property.

Franci Penov
i have tried temp data but it doesnt seem to work ...
lucky
A: 

I used a view to take the data from the user and then saved it to a static variable and then used this variable to pass the data to all the other views .

Thanks anyways

lucky
Terrible!!! solution. Every user will get the same static variable set and if another user accesses the page in between the redirect of a second user then the second user will get the first's value. DO NOT USE THIS ANYONE.
Graphain