0

Alright, I lost the entire morning on this and I'm getting nowhere.

I have a good experience with MVC, been using it since the first betas back in 2008 but I can' t really figure this out.

I substantially have 2 GET methods: the first one renders a form. The second one is the method the form points to. I'm submitting using GET because it's a search form, and I want to have a bookmarkable URL with the parameters.

Something like this

[HttpGet]
public ActionResult DisplayForm()
{
    Contract.Ensures(Contract.Result<ActionResult>() != null);
    Contract.Ensures(Contract.Result<ActionResult>() is ViewResult);

    return this.View();
}

[HttpGet]
public ActionResult Search(MyViewModel viewModel)
{
    Contract.Requires<ArgumentNullException>(viewModel != null);
    Contract.Ensures(Contract.Result<ActionResult>() != null);
    Contract.Ensures(Contract.Result<ActionResult>() is ViewResult || Contract.Result<ActionResult>() is RedirectToRouteResult);

    var result = this.validator.Validate(viewModel); //FluentValidation validation
    if (!result.IsValid)
    {
        result.FillModelState(this.ModelState); //extension method that uses AddModelError underneath for ValidationMessageFor helpers on search form
        return this.RedirectToAction(c => c.DisplayForm()); //MvcContrib redirect to action
    }

    ViewData.Model = viewModel;
    return View();
}

The first time I submit the form, the viewData in the target method gets populated correctly.

If I go back and do another search, it's like the modelbinder "cached" the data I submitted the first time: the viewData always has the values from the first search. It resets only restarting the application.

I tried checking both ModelState and HttpContext.Request and they DO have the new data in them (not stale) but still the viewData gets populated with old data. I also tried overriding OnActionExecuting and OnActionExecuted simply to put a breakpoint in there and check the ModelState in those steps, and found nothing weird.

I also tried to call the Search method directly via the browser bar, since it's in GET and I can do it. Still, ModelState and Request contain the data I input, but the viewData has old data.

This is really getting to my nerves as I can't really understand what's going on.

Any help would really be appreciated.

Matteo Mosca
  • 7,380
  • 4
  • 44
  • 80

2 Answers2

0

Have you tried ModelState.Clear()

When you're done with the search call?

Daniel
  • 622
  • 4
  • 6
  • I tried that, and still no good. Besides the modelstate contains the right data as I stated. It looks like a modelbinder problem, but I can't really say. – Matteo Mosca Nov 30 '12 at 13:44
0

I experimented a lot and I found out that the problem was on an actionfilter on a base class which I was unaware of. The "PassParametersDurignRedirect" filter of MvcContrib. Without it everything works fine.

Matteo Mosca
  • 7,380
  • 4
  • 44
  • 80