开发者

RedirectToRoute with Parameters Objects

开发者 https://www.devze.com 2023-02-20 02:13 出处:网络
I am in controller A and is using redirectToRoute to get to controller B where I am passing two object parameters but I notice these values are null on redirection.RedirectToRoute does not allow the p

I am in controller A and is using redirectToRoute to get to controller B where I am passing two object parameters but I notice these values are null on redirection.RedirectToRoute does not allow the pas开发者_C百科sing of objects?????


When redirecting you can only pass simple scalar values and not complex objects. Example:

public ActionResult Index()
{
    SomeModel model = ...
    return RedirectToAction("Foo", new 
    {
        Prop1 = model.Prop1,
        Prop2 = model.Prop2,
        Prop3 = model.Prop3,
    });
}

public ActionResult Foo(SomeModel model)
{
    // model.Prop1, model.Prop2 and model.Prop3 will be correctly bound here
    ...
}

Another possibility is to persist the object to your datastore and pass only the corresponding id:

public ActionResult Index()
{
    SomeModel model = ...
    string id = Repository.Save(model);
    return RedirectToAction("Foo", new { id = id });
}

public ActionResult Foo(string id)
{
    // fetch the model from repository:
    SomeViewModel model = Repository.Load(id);
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消