Is it possible to RedirectToAction passing data without using parameters in an ASP.NET MVC project?

asp.net, asp.net-mvc, asp.net-mvc-4, c#

Solution

You can't send data with a `RedirectAction`. That's because you're doing a `301` redirection and that goes back to the client.

So better use `TempData`

Assuming you will have model to `createperson` with following properties:

public class CreatePersonData
{
     public string name {get; set;}
     public string address {get; set;}
}

Now fill the `model` and store in `TempData`

CreatePersonData person=new CreatePersonData();
person.name="SomeName";
person.address="SomeAddress";
TempData["person"]=person;

return RedirectToAction("CreatePerson", "Home")

Now while receiving just receive it from `tempdata` and pass the filled `model` to the `view`

public ActionResult CreatePerson()
{
    CreatePersonData person=new CreatePersonData()
    var loadPerson= TempData["person"];
    person = loadPerson;
    return View(person);
}

UPDATE

As @StephenMuecke made a point of loosing data with `TempData` you might need to use `.Keep` or `.Peek` with TempData to retain the value for future requests

Ex:

with `.Peek`

//PEEK value so it is not deleted at the end of the request
var loadPerson= TempData.Peek("person");

or with `.Keep`

//get value marking it from deletion
var loadPerson = TempData["person"];
//later on decide to keep it
TempData.Keep("person");

or as @Stephen said just pass the `id` and select the `user` from database

Ex:

return RedirectToAction("CreatePerson", "Home", new { ID = User.ID });

Now in your `CreatePerson` `ActionResult` just get it from db as below:

public ActionResult CreatePerson(int ID)
{
    CreatePersonData person=new CreatePersonData();
    var user=(from u in tbl_user select u where u.ID=ID);
    person.name=user.name;
    person.address=user.address;
    return View(person);
}

UPDATE 2

You can combine both of the above approaches like storing data in `TempData` and passing the `ID` with `routeValues` and check if `TempData` isn't null then fallback to retrieval of data using `ID` approach.

Ex:

public class CreatePersonData
{
     public string Id{get; set;}
     public string name {get; set;}
     public string address {get; set;}
}

public ActionResult CreatePerson(int ID)
{
    CreatePersonData person=new CreatePersonData();
    var loadPerson=(CreatePersonData)TempData.Peek("person"); //cast the object from TempData
    if(loadPerson!=null && loadPerson.Id==ID)
    { 
        person=loadPerson;
    }
    else
    {
         var user=(from u in tbl_user select u where u.ID=ID);
         person.name=user.name;
         person.address=user.address;
    }
    return View(person);
}

Problem

In my controller of an ASP.NET MVC project, I have a ``` return RedirectToAction("CreatePerson", "Home") ``` This View is a form that creates a person and that works fine. However, I want to RedirectToAction and pre-fill the form with data collected from a form that creates a User for the system. How would I pass the data from the CreateUser form in the CreatePerson form? I know that I could use parameters, but would this really be the best method if most of the time I am calling the CreatePerson view without needing those parameters. Any help in the right direction would be greatly appreciated.

Original source