4

I know, a similar question is asked before. But I couldn't find exact similar case. I want to pass model object in return RedirectToAction() to method in another controller. My code:

  MyModel mdl_obj=new MyModel();
  return RedirectToAction("mthdInAnotherController", "AnotherControllerName",new { mdl_obj = mdl_obj });

I am assigning some values to model object and sending it to method in another controller. But that method has more than one arguments and I am not passing those. While passing, model object is not null but in another controller , I am getting null value for model object. What could be the reason?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jass
  • 341
  • 3
  • 12

2 Answers2

1

TempData is for this scenario. TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller.

The usage is

    public ActionResult Foo()
    {
        // Store data into the TempData that will be available during a single redirect
        TempData["Model"] = new MyModel();
    
        // If you store something into TempData and redirect to a controller action that will be available to consume the data
        return RedirectToAction("bar");
    }
    
    public ActionResult Bar()
    {
        var mdl_obj = TempData["Model"];
        ...
    }

Reference: ViewBag, ViewData and TempData

Epic Chen
  • 1,282
  • 12
  • 17
  • Thank you for your answer . I solved my problem using Session variable.But yes, TempData will also work. – Jass May 27 '21 at 03:09
  • 1
    If you don't need the model during future requests, session variable is the last choice. Always avoid using session. It will consume the memory of the server, that will be a problem if there are too many users. – Epic Chen May 27 '21 at 03:49
  • Ok @Epic Chen ,I'll keep that in mind. Thanks – Jass May 27 '21 at 15:22
  • wouldn't you want `return RedirectToAction("bar");` to be capitalized to return RedirectToAction("Bar"); – AlThePal78 Sep 21 '21 at 02:32
  • Have you try it? It is the same. Moreover, the point is on the method. Sometimes, programmers will expect the url keep in lower case. – Epic Chen Sep 27 '21 at 14:39
1

You will first need to check that the model class is declared within your HTML view like this:

@model MyModel

Without it the view cannot bind data from the controller.

The model class declared in your view must match the class returned by the controller method:

[HttpGet]
public ActionResult mthdInAnotherController(string param1, string param1, ..., paramN)
{
    MyModel = new MyModel();
    // populate model here
    ...
    ...
    return View(myModel);
}

Also check the controller method called has matching input parameter names from your RedirectToAction() parameter object:

return RedirectToAction("mthdInAnotherController", "AnotherControllerName", 
    new { 
        param1 = val1, param2 = val2, ... ,paramN = valN 
    });
Andrew Halil
  • 1,195
  • 15
  • 15
  • 21