传递复杂对象的重定向的ASP.NET MVC? [英] Pass complex object with redirect in ASP.NET MVC?

查看:166
本文介绍了传递复杂对象的重定向的ASP.NET MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的动作:

I have a action that looks like this :

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

该AdRegister是一个复杂的类,我需要到一个重定向方法在注册行为进一步向下传递这个,像这样的:

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)

我知道我可以通过简单的参数,但是在这种情况下,it'sa复杂的对象。这个例子不工作,adRegister's属性将是空的。

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

详细信息:注册行动将采取adRegister并做就可以了SOM神奇,那么它会被发送到验证行动。该验证操作将返回一个确认页面给用户。当用户击中批按钮adRgister将从形式被填充,然后送到vValidate交在那里将被保存。我特地在暂时放置在缓存或数据库中的adRegister但它会更好,如果我能简单传递给下一个动作。

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.

推荐答案

一种可能是通过查询字符串的简单的属性:

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
    }
);

另一种可能性是将其存储在TempData的(对于重定向的寿命)或会话(对于ASP.NET会话的生命周期):

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");

,然后从TempData的检索:

and then retrieve it from TempData:

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

另一种可能性(这也是我建议你)是坚持POST方法这个对象在数据存储:

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);
    ...
}

这篇关于传递复杂对象的重定向的ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆