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 se开发者_开发技巧em 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
}
The last line of the TryUpdateModel
source code is:
return ModelState.IsValid;
...which pretty much answers your question. :)
精彩评论