0

我有一个 ASP.NET MVC4 向导。为了逐步传递一个大视图模型,我使用了期货程序集。我序列化我的模型

@Html.Serialize("model", Model, SerializationMode.Signed); 

并在控制器中反序列化它

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var serialized = Request.Form["model"];
    if (serialized != null)
    {
        model = (BausparViewModel)new MvcSerializer().Deserialize(serialized, SerializationMode.Signed);
        TryUpdateModel(model);
    }

...
}

我在

protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
    if (filterContext.Result is RedirectToRouteResult)
        TempData["model"] = model;
}

每个向导步骤都有自己的视图,我使用 RedirectToAction("ActionName") 在控制器中管理上一个/下一个操作

一切正常,只要使用 F5 或菜单没有刷新浏览器。在这一点上,控制器再次被调用。控制器已经有一个模型,尽管 TempData 没有再次保存。

但是缺少两个枚举属性。它们在前面的步骤中使用。他们通过 RadiobuttonFor 绑定到视图。

那么为什么会有一个不完整的模型呢?

在此先感谢 csteinmueller

4

1 回答 1

0

For anything that should live longer than 1 request, you should not use TempData. Use Session or some other longer term storage mechanism.

TempData is designed to delete a value after it is read out of the dictionary. That's why TempDataDictionary has methods like Peek and Keep. Those methods let you tell the dictionary explicitly "hey, don't delete this value after I read it", because by default, it does delete the value after you read it.

于 2013-01-08T15:53:16.317 回答