4

私のコントローラコードは次のようになります:

[HttpPost]
public ActionResult Create(ExampleViewModel model)
{
    model.User.RegistrationNumber = this.RegistrationNumber;

    if (ModelState.IsValid)
    {

    }

    return View("Create", model);
}

"Registration Number cannot be blank"まだ明示的に設定していることを示す検証エラーメッセージが表示され続けます。

ModelStateモデルを何らかの方法で変更したため、リセットする必要がありますか?

基本的にテキストボックスを無効に設定すると、フォームの投稿中にデータが失われたため、明示的に再度設定する必要があります。

4

4 に答える 4

0

ここでの他のソリューションは .NET Core 3.1 では期待どおりに機能しなかったため、目的のモデル プロパティのみの検証状態を明示的に設定する次の代替手段を使用しました。

if (ModelState["Property"].Errors.Count > 0)
{
    model.Property = someRequiredProperty; // db query using FirstOrDefault()

    if (model.Property != null)
    {
        ModelState["Property"].ValidationState = ModelValidationState.Valid;
    }
}

if (ModelState.IsValid)
{
    // ...
}
于 2020-09-17T23:38:00.343 に答える