与redirectAction和PRG模式发送操作之间的数据 [英] send data between actions with redirectAction and prg pattern

查看:97
本文介绍了与redirectAction和PRG模式发送操作之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何操作间与redirectAction发送数据??

我使用PRG模式。我想让这样的事情

  [HTTPGET]
    [ActionName(成功)]
    公众的ActionResult成功(PersonalDataViewModel模型)
    {
        //模型阁
        如果(型号== NULL)
            返回RedirectToAction(指数,账户);        //模型确定
        返回查看(模型);
    }    [HttpPost]
    [ExportModelStateToTempData]
    [ActionName(成功)]
    公众的ActionResult SuccessProcess(PersonalDataViewModel模型)
    {        如果(!ModelState.IsValid)
        {
            ModelState.AddModelError(,错误);
            返回RedirectToAction(指数,账户);
        }        //模型确定
        返回RedirectToAction(成功,新PersonalDataViewModel(){BadgeData = this.GetBadgeData});
    }


解决方案

在重定向你只能通过查询字符串值。而不是整个复杂的对象:

 返回RedirectToAction(成功,新{
    为prop1 = model.Prop1,
    prop2 = model.Prop2,
    ...
});

这仅适用于标量值。所以,你需要确保你每包含您在查询字符串需要的财产,否则将在重定向丢失。

另一种可能性是坚持某个模型的服务器上(如数据库或东西),只有通过重定向的ID,这将使检索模型回来的时候:

  INT ID = StoreModel(模型);
返回RedirectToAction(成功,新{ID = ID});

和成功的行动内检索模型回:

 公众的ActionResult成功(INT ID)
{
    VAR模型= GetModel(ID);
    ...
}

另一种可能性是使用的TempData 虽然我个人不建议这样做:

 的TempData [模型] =模型;
返回RedirectToAction(成功);

成功操作,从里面取出它的TempData

  VAR模型= TempData的[模型]作为PersonalDataViewModel;

how can i send data between actions with redirectAction??

I am using PRG pattern. And I want to make something like that

[HttpGet]
    [ActionName("Success")]
    public ActionResult Success(PersonalDataViewModel model)
    {
        //model ko
        if (model == null)
            return RedirectToAction("Index", "Account");

        //model OK
        return View(model);
    }

    [HttpPost]
    [ExportModelStateToTempData]
    [ActionName("Success")]
    public ActionResult SuccessProcess(PersonalDataViewModel model)
    {

        if (!ModelState.IsValid)
        {
            ModelState.AddModelError("", "Error");
            return RedirectToAction("Index", "Account");
        }

        //model OK
        return RedirectToAction("Success", new PersonalDataViewModel() { BadgeData = this.GetBadgeData });
    }

解决方案

When redirect you can only pass query string values. Not entire complex objects:

return RedirectToAction("Success", new {
    prop1 = model.Prop1,
    prop2 = model.Prop2,
    ...
});

This works only with scalar values. So you need to ensure that you include every property that you need in the query string, otherwise it will be lost in the redirect.

Another possibility is to persist your model somewhere on the server (like a database or something) and when redirecting only pass the id which will allow to retrieve the model back:

int id = StoreModel(model);
return RedirectToAction("Success", new { id = id });

and inside the Success action retrieve the model back:

public ActionResult Success(int id)
{
    var model = GetModel(id);
    ...
}

Yet another possibility is to use TempData although personally I don't recommend it:

TempData["model"] = model;
return RedirectToAction("Success");

and inside the Success action fetch it from TempData:

var model = TempData["model"] as PersonalDataViewModel;

这篇关于与redirectAction和PRG模式发送操作之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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