Can I perform some processing on the POST data before ASP.NET MVC UpdateModel happens?

Posted by Domenic on Stack Overflow See other posts from Stack Overflow or by Domenic
Published on 2010-05-19T14:46:29Z Indexed on 2010/05/19 14:50 UTC
Read the original article Hit count: 354

I would like to strip out non-numeric elements from the POST data before using UpdateModel to update the copy in the database. Is there a way to do this?

// TODO: it appears I don't even use the parameter given at all, and all the magic
// happens via UpdateModel and the "controller's current value provider"?
[HttpPost]
public ActionResult Index([Bind(Include="X1, X2")] Team model) // TODO: stupid magic strings
{
    if (this.ModelState.IsValid)
    {
        TeamContainer context = new TeamContainer();

        Team thisTeam = context.Teams.Single(t => t.TeamId == this.CurrentTeamId);
        // TODO HERE: apply StripWhitespace() to the data before using UpdateModel.
        // The data is currently somewhere in the "current value provider"?
        this.UpdateModel(thisTeam);
        context.SaveChanges();

        this.RedirectToAction(c => c.Index());
    }
    else
    {
        this.ModelState.AddModelError("", "Please enter two valid Xs.");
    }

    // If we got this far, something failed; redisplay the form.
    return this.View(model);
}

Sorry for the terseness, up all night working on this; hopefully my question is clear enough? Also sorry since this is kind of a newbie question that I might be able to get with a few hours of documentation-trawling, but I'm time-pressured... bleh.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about updatemodel