aplon
aplon

Reputation: 477

Weird Behaviour with models

So this is strange. I have a model composed of person and List<person>. when the user submits the view back to the controller, person is add to the List<person> and then we clear out all the person fields by the means of a person=new Person();.

I would expect that when return to the view, all the person fields are cleared out and the view is a "fresh" start. However, for some strage reason I cannot figure out, most of the fields for person are still filled out with the previous values (even after the person=new Person();).

The model is a complex model, composed of several "objects of objects", and some of the objects inherit from other objects. Still, I can't understand why the view still shows values from previous posts.

EDIT !!!!!

I am posting via a regular form post (HTML.BeginForm()). So here is my controller:

    [HttpPost]
    [ValidateAntiForgeryToken()]
    public ActionResult sendInscriptorRequest(inscriptionModel _model)
    {
        var _umbracoModel = Umbraco.TypedContentAtRoot().FirstOrDefault();
        _model = bllInscripcion.fillModel(_model);
        _model = _model.Map(_umbracoModel);
        if (_model.formAction == "addParticipants")
        {
            _model.participants.Add(_model.newParticipant);
            _model.newParticipant = new participantModel();
            _model.ui.participants.btnTotalParticipantsNumber += 1;
            return View("addParticipants", _model);
        }
        else
        {
            _model.newParticipant = bllInscripcion.preFillParticipantContactWithInscriptorContact(_model);
            return View("formularioInscripcion2", _model);
        }
    }

Upvotes: 0

Views: 65

Answers (2)

Craig W.
Craig W.

Reputation: 18155

You trying to return the view with the model from the POST action method. That won't work because the intended behavior is to return the model state that was posted so the user can make corrections.

If you're posting data and then want to return an empty view you should redirect to the GET action method that returns the initial state of the view. It's known as a Post-Redirect-Get pattern.

http://en.wikipedia.org/wiki/Post/Redirect/Get

Following this pattern will also solve the problem of the user refreshing the page following the POST and getting that annoying dialog that says "You're about to resubmit data. Are you sure?" to which the user always responds "Yes" and then your system has to deal with the potential duplication.

Upvotes: 3

Mathew Thompson
Mathew Thompson

Reputation: 56429

Explanation of the problem:

This is because of ModelState. By default, MVC will retain the model state within a post action if you're returning the same view. To prevent this, simply clear it before returning the view:

ModelState.Clear();

This will mean that whatever model you pass back to the view will be used, not the one stored in model state.

DISCLAIMER:

You should use ModelState.Clear only when needed. Judging by your code, you don't need to use it. Simply redirecting to another page after you have performed the necessary logic is the better thing to do here (as per the PRG pattern).

Upvotes: 2

Related Questions