Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to gain control over model binding?

I started using MVC recently and I'm getting little disappointed. Instead of helping me, the framework is getting in my way.

I'm trying to write a controller action like this (pseudo code)

ActionResult Save(long id, string whichForm)
{
    if (whichForm == "A")
    {
        var vm = CreateModel(Request.Form);
        if (!TryValidate(vm))
            return View(vm);
        else
            return RedirectToRoute("Success");
    }
    else ....
}

Basically I'd like to have control over when my view-model is constructed and when it is validated. Is this possible? How can I implement CreateModel method? Consider I may want to create several different view-models within this controller action.

*Rant: I don't really understand why view-model binding and validation are mixed together in DefaultModelBinder. Seems like code smell. Specially when it's hard to override this behaviour.

like image 977
Kugel Avatar asked May 31 '12 00:05

Kugel


1 Answers

You can create and bind to an existing model at your discretion:

public ActionResult Save(long id, string whichForm)
{
    if (whichForm == "A")
    {
        var vm = new FormAViewModel();

        if (!TryUpdateModel(vm))
            return View(vm);
        else
            return RedirectToRoute("Success");
    }
    // else ....
}

You also have the option of creating your own IModelBinder, if you want complete control over the binding process. You can replace the default model binder, or you can register specific IModelBinder implementations for specific types. I would suggest, however, that unless your binding logic is simple, you will probably want to derive your custom model binder from DefaultModelBinder and just override the parts you don't like.

I hate to leave a troll-ish comment, but 9 times out of 10 the reason someone feels a framework is getting in their way is because they don't yet understand how to properly use it. Here is an article with general tips on model binding.

As to your rant: Validation and Binding are separate, however, the default model binder does trigger validation. The reason for this is to allow your application to gracefully handle problems binding to missing/invalid/incomplete values, rather than allowing binding to fail silently or throwing exceptions.

like image 146
moribvndvs Avatar answered Sep 19 '22 01:09

moribvndvs