0

我需要在我的控制器中验证 mvc 中的一些东西我必须

@Html.ValidationMessageFor(model => model.somestring)// in the view

if (model.string = some condition)
ModelState.AddModelError("somestring", "String cannot be empty");// in the controller

但是如果在我看来我有一个自定义对象,例如

@Html.ValidationMessageFor(model => model.someobject.somestring)// in the view

我如何验证它?以下语法是否正确?

if (model.someobject.somestring = some condition)
ModelState.AddModelError("somestring", "String cannot be empty");// in the controller
4

1 回答 1

4

您需要确保在指定密钥时使用属性的完整路径:

ModelState.AddModelError("someobject.somestring", "String cannot be empty);
于 2013-07-15T16:39:37.717 回答