Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:25:21+00:00 2026-06-06T11:25:21+00:00

In the RedirectToAction below, I’d like to pass a viewmodel . How do I

  • 0

In the RedirectToAction below, I’d like to pass a viewmodel. How do I pass the model to the redirect?

I set a breakpoint to check the values of model to verify the model is created correctly. It is correct but the resulting view does not contain the values found in the model properties.

//
// model created up here...
//
return RedirectToAction("actionName", "controllerName", model);

ASP.NET MVC 4 RC

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:25:23+00:00Added an answer on June 6, 2026 at 11:25 am

    RedirectToAction returns a 302 response to the client browser and thus the browser will make a new GET request to the url in the location header value of the response came to the browser.

    If you are trying to pass a simple lean-flat view model to the second action method, you can use this overload of the RedirectToAction method.

    protected internal RedirectToRouteResult RedirectToAction(
        string actionName,
        string controllerName,
        object routeValues
    )
    

    The RedirectToAction will convert the object passed(routeValues) to a query string and append that to the url(generated from the first 2 parameters we passed) and will embed the resulting url in the location header of the response.

    Let’s assume your view model is like this

    public class StoreVm
    {
        public int StoreId { get; set; }
        public string Name { get; set; }
        public string Code { set; get; } 
    }
    

    And you in your first action method, you can pass an object of this to the RedirectToAction method like this

    var m = new Store { StoreId =101, Name = "Kroger", Code = "KRO"};
    return RedirectToAction("Details","Store", m);
    

    This code will send a 302 response to the browser with location header value as

    Store/Details?StoreId=101&Name=Kroger&Code=KRO
    

    Assuming your Details action method’s parameter is of type StoreVm, the querystring param values will be properly mapped to the properties of the parameter.

    public ActionResult Details(StoreVm model)
    {
      // model.Name & model.Id will have values mapped from the request querystring
      // to do  : Return something. 
    }
    

    The above will work for passing small flat-lean view model. But if you want to pass a complex object, you should try to follow the PRG pattern.

    PRG Pattern

    PRG stands for POST – REDIRECT – GET. With this approach, you will issue a redirect response with a unique id in the querystring, using which the second GET action method can query the resource again and return something to the view.

    int newStoreId=101;
    return RedirectToAction("Details", "Store", new { storeId=newStoreId} );
    

    This will create the url Store/Details?storeId=101
    and in your Details GET action, using the storeId passed in, you will get/build the StoreVm object from somewhere (from a service or querying the database etc)

    public ActionResult Details(string storeId)
    {
       // from the storeId value, get the entity/object/resource
       var store = yourRepo.GetStore(storeId);
       if(store!=null)
       {
          // Map the the view model
          var storeVm = new StoreVm { Id=storeId, Name=store.Name,Code=store.Code};
          return View(storeVm);
       }
       return View("StoreNotFound"); // view to render when we get invalid store id
    }
    

    TempData

    Following the PRG pattern is a better solution to handle this use case. But if you don’t want to do that and really want to pass some complex data across Stateless HTTP requests, you may use some temporary storage mechanism like TempData

    TempData["NewCustomer"] = model;
    return RedirectToAction("Index", "Users");
    

    And read it in your GET Action method again.

    public ActionResult Index()
    {      
      var model=TempData["NewCustomer"] as Customer
      return View(model);
    }
    

    TempData uses Session object behind the scene to store the data. But once the data is read the data is terminated.

    Rachel has written a nice blog post explaining when to use TempData /ViewData. Worth to read.

    Using TempData to pass model data to a redirect request in Asp.Net Core

    In Asp.Net core, you cannot pass complex types in TempData. You can pass simple types like string, int, Guid etc.

    If you absolutely want to pass a complex type object via TempData, you have 2 options.

    1) Serialize your object to a string and pass that.

    Here is a sample using Json.NET to serialize the object to a string

    var s = Newtonsoft.Json.JsonConvert.SerializeObject(createUserVm);
    TempData["newuser"] = s;
    return RedirectToAction("Index", "Users");
    

    Now in your Index action method, read this value from the TempData and deserialize it to your CreateUserViewModel class object.

    public IActionResult Index()
    {
       if (TempData["newuser"] is string s)
       {
           var newUser = JsonConvert.DeserializeObject<CreateUserViewModel>(s);
           // use newUser object now as needed
       }
       // to do : return something
    }
    

    2) Set a dictionary of simple types to TempData

    var d = new Dictionary<string, string>
    {
        ["FullName"] = rvm.FullName,
        ["Email"] = rvm.Email;
    };
    TempData["MyModelDict"] = d;
    return RedirectToAction("Index", "Users");
    

    and read it later

    public IActionResult Index()
    {
       if (TempData["MyModelDict"] is Dictionary<string,string> dict)
       {
          var name = dict["Name"];
          var email =  dict["Email"];
       }
       // to do : return something
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As indicated (by another poster) here: Can you pass a model with RedirectToAction? I
How do I redirect to another action in my controller correctly? When the redirecttoaction
If I have situation like below, where I success. handle image upload and store
I created a controller which save files. The below code is a part of
I have a ViewModel with a property as below: [DisplayName(As Configured On:)] [DisplayFormat(DataFormatString={0:d}, ApplyFormatInEditMode=true)]
Below, in CreateTest, uponsuccessful, I want to redirect to Tests from CreateTest. I want
I am building a fluent interface and would like to call the code below
I have something like the following code you see below as a template. what
iam having controller like below which is not registered in routes table public class
I have a strongly typed view that is using a viewmodel I created. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.