Hacker News new | ask | show | jobs
by mattmanser 3059 days ago
I find myself using TryUpdateModel far more these days than relying on letting the controller bind automatically.

It's one of those magic features that ultimately hides too much.

Far too much can go wrong and the error get silently swallowed, plus having a parameterless constructor is rare for complex objects. So you end up with a model missing properties you need filled in if there's a validation error, extra work to make sure it's properly populated that should have happened in the constructor, which means code duplication.

1 comments

I agree! I find this especially useful when I have to set things like a UserId associated with an entity or with more complex entities where some data comes from injected services. I know you can create a service that handles creating the entity as an abstraction, but that gets repetitive and adds complexity in large projects. I might actually add this my article :)!