tags:

views:

126

answers:

1

Hi
I'm fooling around with the NerdDinner tutorial Dinner Edit control.
I get a FormCollection as one of the arguments, can I trim the data in it before I use UpdateModel().

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, FormCollection formValues)
    {
        Dinner dinner = dinnerRepository.GetDinner(id);
        try
        {
            UpdateModel(dinner);
            dinnerRepository.Save();
            return RedirectToAction("Details", new { id = dinner.DinnerID });
        }
        catch
        {
            foreach (var issue in dinner.GetRuleViolations())
            {
                ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
            }
            return View(dinner);
        }
    }

Or do I have to do that manually by iterating over the Request.Form keys?

+2  A: 

Instead of using the raw form values, you could use a model binder to bind to a custom object.

You can create your own model binder by implementing the IModelBinder interface. In IModelBinder.BindModel method you could trim or do any other string manipulation you want.

Once this is done, your action will receive the data formatted the way you want it.

For more info, K Scott Allen and Scott Hanselman have a few articles that cover IModelBinder.

nikmd23
The Hanselman link is http://www.hanselman.com/blog/IPrincipalUserModelBinderInASPNETMVCForEasierTesting.aspx
nikmd23