Вопрос

I have a ASP.NET MVC4 wizard. To pass one big viewmodel from step to step I use the futures assembly. I serialize my model with

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

and deserialize it in the controllers

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

...
}

I set the TempData in the

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

Every wizard step has it's own view and I manage previous/next actions in the controller with RedirectToAction("ActionName")

Everything works fine, as long as the browser gets no refresh by using F5 or the menu. On this point the controller gets called again. The controller already has a model,although the TempData doesn't get saved again.

But there are two enum properties missing. They are used in previous steps. They got bound to the view via RadiobuttonFor.

So why is there a model which is incomplete?

Thanks in advance csteinmueller

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top