加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用ASP.NET MVC中的重定向传递复杂对象?

发布时间:2020-12-15 17:48:27 所属栏目:百科 来源:网络整理
导读:嗨, 我有一个这样的动作: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Register(AdRegister adRegister,IEnumerableHttpPostedFileBase files) AdRegister是一个复杂的类,我需要在注册操作中进一步将其传递给重定向方法,如下所示: return this.Red
嗨,

我有一个这样的动作:

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

AdRegister是一个复杂的类,我需要在注册操作中进一步将其传递给重定向方法,如下所示:

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

验证操作如下所示:

public ActionResult Validate(AdRegister adRegister)

我知道我可以传递简单的参数,但在这种情况下它是一个复杂的对象.此示例不起作用,adRegister的属性将为空.

这是可能的,如果是这样,怎么样?

最好的祝福

更多信息:注册动作将采取adRegister并做一些魔法,然后它将被发送到验证操作.验证操作将返回验证页面给用户.当用户按下授权按钮时,adRgister将从该表单中填写,然后发送到将被保存的vValidate帖子.我已经查看了adRegister在缓存或数据库中临时放置,但如果我可以简单地将其传递给下一个动作,将会更好.

解决方法

一种可能性是传递查询字符串中的简单属性:
return RedirectToAction(
    "Validate",new { 
        foo = adRegister.Foo,bar = adRegister.Bar,... and so on for all the properties you want to send
    }
);

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

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

然后从TempData中检索它:

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

另一个可能性(和我建议你的一个)是在数据存储区的POST方法中保留此对象:

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

然后在重定向后从数据存储中取出它:

public ActionResult Validate(string id)
{
    AdRegister adRegister = Repository.Get(id);
    ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读