Pass complex object with redirect in ASP.NET MVC?

2019-01-20 08:11发布

Hi,

I have a action that looks like this :

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register(AdRegister adRegister, IEnumerable<HttpPostedFileBase> files)

The AdRegister is a complex class and I need to pass this in to a redirect method further down in the Register action, like this :

return this.RedirectToAction("Validate", adRegister);

The Validate action looks like this :

public ActionResult Validate(AdRegister adRegister)

I do know that I can pass simple parameters but in this case it´s a complex object. This example do not work, the adRegister´s properties will be null.

Is this posible and if so, how?

BestRegards

More Information : Register action will take the adRegister and do som magic on it, then It will be sent to the Validate action. The Validate action will return a validation page to the user. When the user hit the grant button the adRgister will be filled from the form and then sent to the vValidate post where it will be saved. I have looked in to place the adRegister in cache or database temporarily but it will be better if I could simple pass it to the next action.

3条回答
小情绪 Triste *
2楼-- · 2019-01-20 08:49

an idea would probably create a session variable and pass around a Key that references that session variable if the object is required acorss a few views?

查看更多
叼着烟拽天下
3楼-- · 2019-01-20 08:53

ASP.NET MVC's tempdata should be perfect for this.

That said, TempData or Session is one option, but has some downsides like being quite violate and oftentimes murky or difficult to debug. What might be preferable is to "stash" the temporary value in a persistent store, such as the user's profile or your own database, then pass a key through the validate method which can then load the data from said store. This also opens up the possibility of recovering abandoned carts and such.

查看更多
我只想做你的唯一
4楼-- · 2019-01-20 08:58

One possibility would be to pass the simple properties in the query string:

return RedirectToAction(
    "Validate", 
    new { 
        foo = adRegister.Foo, 
        bar = adRegister.Bar, 
        ... and so on for all the properties you want to send
    }
);

Another possibility is to store it in TempData (for the lifetime of the redirect) or Session (for the lifetime of the ASP.NET session):

TempData["adRegister"] = adRegister;
return RedirectToAction("Validate");

and then retrieve it from TempData:

public ActionResult Validate()
{
    adRegister = TempData["adRegister"] as AdRegister;
    ...
}

Yet another possibility (and the one I would recommend you) is to persist this object in the POST method in your datastore:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(AdRegister adRegister, IEnumerable<HttpPostedFileBase> files)
{
    ...
    string id = Repository.Save(adRegister);
    return RedirectToAction("Validate", new { id = adRegister.Id });
}

and then fetch it from the data store after you redirect:

public ActionResult Validate(string id)
{
    AdRegister adRegister = Repository.Get(id);
    ...
}
查看更多
登录 后发表回答