views:

311

answers:

1

Doing validation in my binder, I'm wondering if there's a need to check the return value. In Option 1 below, is there ever going to be a difference in case 1 and case 2? It doesn't seem possible that TryUpdateModel would return true, but ModelState.IsValid is false.

Option 1:

  if (TryUpdateModel(editItem, new string[] { "Field" }))
  {
    if (ModelState.IsValid)
    {
    } else {
    // Invalid model case 1
  }
  } else {
    // Invalid model case 2
  }

Option 2:

  TryUpdateModel(editItem, new string[] { "Field" }))
  if (ModelState.IsValid)
  {
  } else {
    // only one invalid model case 
  }
+6  A: 

The last line of the TryUpdateModel source code is:

        return ModelState.IsValid;

...which pretty much answers your question. :)

Craig Stuntz
Actually, I'd want a more thorough answer. Do I need to use `TryUpdateModel` if I use the automagic binding in ASP.NET MVC 2 or is it pretty much obsoleted except for corner cases?
Deniz Dogan
You're asking an unrelated question. You don't need it, though.
Craig Stuntz