I have 2 controllers: AliceController
and BobController
. Each contains Index
action method.
The viewmodel is simple as follows:
class Foo{[Required]public int data{get;set;}}
How to pass the strongly typed object from Alice to Bob?
class AlliceController: Controller
{
[HttpPost]
public ActionResult Index(Foo foo)
{
if(ModelState.IsValid)
{
// I want to pass foo to Bob's Index. How to do it?
return RedirectToActio开发者_JAVA技巧n("Index","Bob");
}
else return View();
}
}
In theory, you can use the RouteValueDictionary
parameter to RedirectToAction
:
return RedirectToAction("Index", "Bob", new { param = value });
However, you cannot pass an object*via this type of redirection, only primitive types that can be tacked onto the GET url (int, string, etc).
Use TempData instead.
That being said, i have no idea what you are attempting to do.
Some concerns:
- Why not just pass the actual value, instead of the entire ViewModel?
- Why are you redirecting between controllers? (this can be done, just don't see the reason for it in your scenario).
Can't you just instantiate a BobController and return the result of the it's index method, passing in Foo?
I am not sure that you can pass a strongly typed object via Redirection.
You can instead pass just parameters
RedirectToAction( new RouteValueDictionary(
new{
controller = "Blah",
action = "Blah",
foo = "Bar"
}
));
精彩评论