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?
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With