0

I have a registration form in the side bar of my web application. When the user submits the entered data, the user should be redirected to another page with a more complete registration form when he can fill the rest of the data. The data that was entered by the user in the first form should be already there in the second form, but that's not happening... I checked to see the value of the view model I'm passing to the second action method and it was null and in the browser's address bar I get:

http://localhost:2732/User/RegisterPage?model=Sharwe.MVC.ViewModels.RegisterPageViewModel

Here's the code:

    public ActionResult Register()
    {
        return PartialView(new RegisterViewModel());
    }

    [HttpPost]
    public ActionResult Register(RegisterViewModel dto)
    {
        var model = Mapper.Map<RegisterViewModel, RegisterPageViewModel>(dto);
        return RedirectToAction("RegisterPage", "User", new { viewModel = model });
    }

    public ActionResult RegisterPage(RegisterPageViewModel viewModel)
    {
        return View(viewModel);
    }

Isn't that the way to do this? Or am I missing something here...?

Kassem
  • 8,116
  • 17
  • 75
  • 116

2 Answers2

3

The Dictionary passed to RedirectToAction() is the Route Value not the View Model. And RedirectToAction() is basically telling the browser to go to a certain URL. Browser by default makes the GET request and obviously you lose your data.

For this, you need to use TempData dictionary. You can store view model in TempData and then RedirectToAction() to RegisterPage. TempData saves the data for only 1 request span and would delete it automatically. It's ideal for this scenario.

See this for more details > The value for a object inside a viewmodel lost on redirect to action in asp.net mvc 2.0?

Community
  • 1
  • 1
neebz
  • 11,465
  • 7
  • 47
  • 64
  • Ah I see. So how do I get the data from TempData, do I do that manually (i.e: for each field) or can I make of model binding to do it for me? And since TempData is some form of Session, do you really think it's going to work if I still haven't resolved this: http://stackoverflow.com/questions/5844635/session-variables-lost-between-controllers-action-methods – Kassem May 01 '11 at 15:24
1

In this particular case you don't need to use RedirectToAction, you can simply call the RegisterPage action directly:

[HttpPost]
public ActionResult Register(RegisterViewModel dto)
{
    var model = Mapper.Map<RegisterViewModel, RegisterPageViewModel>(dto);
    return RegisterPage(model);
}

public ActionResult RegisterPage(RegisterPageViewModel viewModel)
{
    return View(viewModel);
}
ataddeini
  • 4,931
  • 26
  • 34
  • @ataddeini: did not work... complained about the ViewModel type, and it did not really go to the intended "page". – Kassem May 01 '11 at 15:20
  • @Kassem: OK, not sure why it would complain about the ViewModel type. Was that a runtime error I'm guessing? – ataddeini May 01 '11 at 15:29
  • @Kassem: Right, OK re-reading your scenario it does make more sense to use `TempData` like nEEbz suggested. – ataddeini May 01 '11 at 15:34
  • @Kassem: are the fields named the same in both the short registration form and the long registration form? It could be that it's not binding the model properly because the field names don't match (or something along those lines). – Roman May 01 '11 at 15:38
  • @Kassem: What's the `@model` directive look like on the `RegisterPage` view? If it's not set to use the `RegisterPageViewModel` then you'll probably get the type error regardless. – ataddeini May 01 '11 at 15:44
  • @R0MANARMY: Nope, that's not the problem. They are the same, and I'm using AutoMapper to take care of mapping everything. – Kassem May 01 '11 at 15:56
  • @ataddeini: It is set to RegisterPageViewModel. As I said, the problem is that it's not going to the RegisterPage in the first place. I'll try the TempData trick now. Hopefully it will work. – Kassem May 01 '11 at 15:57