开发者

asp.net mvc: TryUpdateModel return value or ModelState.IsValid?

开发者 https://www.devze.com 2022-12-16 20:41 出处:网络
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开发者_开发技巧e

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. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消