Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass single parameter in RedirectToAction to different controller in ASP .NET MVC?

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?

like image 483
Jass Avatar asked Oct 23 '25 05:10

Jass


1 Answers

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 
    });
like image 150
Andrew Halil Avatar answered Oct 25 '25 20:10

Andrew Halil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!