ASP.NET MVC:如何将嵌套 属性 添加到 Modelstate?

ASP.NET MVC: how to add nested property to Modelstate?

我想在 Modelstate 无效时添加 属性,但是当 [=31] =] 嵌套在另一个里面。

下面是 Visual Studio 已经为我做的。

<td>
    <span class="editor-field">
       @Html.EditorFor(model => model.TheResource.stateProvince)
    </span><span class="editor-validation-error">
       @Html.ValidationMessageFor(model => model.TheResource.stateProvince)
    </span>
 </td>

现在如何将 属性 添加到服务器端的模型状态?

if(model.TheResource.countryName == "US")
{
    if(!GetUSAStates().Contains(stateProvince))
    {
       ModelState.AddModelError("", 
                                "US state or territory not valid." + 
                                "Please check the spelling. Use, for" + 
                                " instance, Maryland instead of MD");
    }
}

到目前为止,我一直在使用空字符串,因为我不知道如何添加 属性。问题是它在摘要中显示错误。

知道如何将嵌套的 属性 添加到 Modelstate 吗?

感谢您的帮助

您可以指定完全限定的 属性 名称作为 .AddModelError

的第一个参数
ModelState.AddModelError("TheResource.stateProvince", ""US state or territory not valid ...");