3

As I was originally learning about MVC, the book I was using taught the following technique for Controller actions:

public ActionResult MethodName(Model model) {
    if (ModelState.IsValid) { }
    else { }
}

However, I quickly ran across an unrelated issue, and someone on Stack Overflow pointed me to a new function in MVC3 that was like this:

public ActionResult MethodName() {
    var model = new Model();

    if (TryUpdateModel(model)) { }
    else { }
}

That new TryUpdateModel() technique did help with a separate issue that I was encountering (aside from the one my question was about), so I was using that for a while. However, I've recently run into a couple of minor issues which caused me to go back to the original with ModelState.IsValid.

Neither bugs have ever been that major, so it just seems to me that this is more of a "best practice" scenario. Those minor bugs might have been from other issues in the code that I've written.

What is everyone's experience been between the two of these methodologies? (For the record, this is with MVC3 and MVC4 in C# 4.0 and Visual Studio 2010.)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.