私はMVCを初めて使用しています。私は慎重に問題を詳述しようとします。フォームがポストされたときModelState.IsValidがfalseを返す場合、ユーザーが投稿したデータを含む同じビューを読み込みます。
[AllowAnonymous]
public ActionResult Register()
{
RegisterViewModel registerModel = new RegisterViewModel();
//Some Code to populate dropdowns for user to select
return View(registerModel);
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
try
{
if (ModelState.IsValid)
{
//insert data in tables
RedirectToAction("Register");
}
AddErrors(result);
}
catch (Exception ex)
{
ModelState.AddModelError("", "Error Processing Your Request");
}
// If ModelState.IsValid returns false show the form with posted data by user
return RedirectToAction("Register", model);
}
今すぐユーザーが満たされたすべてのデータは保持されません。 私はPOSTメソッドであるそのうちの一つ2つのアクションメソッドとコントローラーを持っています。 投稿したデータでユーザーに同じフォームを表示し、 の情報が不足していると登録を完了します。
申し訳ありませんが、これは非常に基本的な質問です。同じビューにデータを永続化する方法
RedirectToAction( "Register"モデル)は、ビューを返し、検証エラー( 'RedirectToAction(...) 'ではなく)を表示するために' return View(model); 'を使用します。 –
return同じ関数をもう一度 – Musab
私はビューにロードするEditorTemplateを持っていて、View(model)を返します。 ** 'EmploymentType'というキーを持つViewDataアイテムは 'System.Int32'タイプですが、 'IEnumerable'タイプでなければなりません。** –
LearningPal