を試してみました本当にあなたの質問に答えるが、ちょうど私のコメントに展開する: - :
public class MyModel
{
[Required]
public string Foo { get; set; } // Populated in step 1
[Required]
public string Bar { get; set; } // Populated in step 2
}
-
それはあなたのようなものを持っているように聞こえます
ユーザーがまだBar
の値を入力していないため、ステップ1をPOSTするときに問題が発生しました。したがって、ModelStateError
があります。
私の好適な解決策ではなく、自分の永続化モデルの検証と周り台無しにしようとするよりも、例えば、各ウィザードステップのためのViewModelを使用してモデルの実装から、あなたのビューの実装を分離するために、次のようになります -
public class MyModel
{
[Required]
public string Foo { get; set; }
[Required]
public string Bar { get; set; }
}
public class StepOneModel
{
[Required]
public string Foo { get; set; }
}
public class StepTwoModel
{
// This really depends on the behaviour you want.
// In this example, the model isn't persisted until
// the last step, but you could equally well persist
// the partial model server-side and just include a
// key in subsequent wizard steps.
[Required]
public StepOneModel StepOne { get; set; }
[Required]
public string Bar { get; set; }
}
@model StepOneModel
@using (html.BeginForm()) {
@html.EditorFor(x => x.Foo);
}
- :あなたは、StepOneビューのようなものに見える
public ActionResult StepOne()
{
return View(new StepOneViewModel());
}
[HttpPost]
public ActionResult StepOne(StepOneViewModel model)
{
if(ModelState.IsValid)
{
var stepTwoModel = new StepTwoViewModel()
{
StepOne = model
};
// Again, there's a bunch of different ways
// you can handle flow between steps, just
// doing it simply here to give an example
return View("StepTwo", model);
}
return View(model);
}
[HttpPost]
public ActionResult StepTwo(StepTwoViewModel model)
{
if (ModelState.IsValid)
{
// You could also add a method to the final ViewModel
// to do this mapping, or use something like AutoMapper
MyModel model = new MyModel()
{
Foo = model.StepOne.Foo
Bar = model.Bar
};
this.Context.MyModels.Add(model);
this.Context.SaveChanges();
}
return View(model);
}
- :
あなたのコントローラのアクションは次のようになり
あなたStepTwoビューは次のようになります - ちょうどモデルの検証をオフに比べて大きな利点は、あなたのViewModelに現在のステップのための検証要件を置くことができるということです
@model StepTwoModel
@using (html.BeginForm("StepTwo")) {
@html.HiddenFor(x => x.StepOne);
@html.EditorFor(x => x.Bar);
}
を - あなたはそのすべてを保証することができますステップ1からの値は、ステップ2に進む前に有効です。あなたはより多くのRESTfulなアプローチをしたい場合は
別の一般的なソリューションは、<のdiv >クライアント側の各ステップをラップすることです(あなたのコントローラは、データがウィザード形式のビューから来ていることを気にしない場合)ユーザーの進行に合わせてjavascriptを使用して表示/非表示にします。
モデルをステップ間で永続化する必要がある場合は、モデルのValidationAttributesとその意味について考える必要があります。 User.DateOfBirth
にRequired
と注釈を付けたが、ステップが入る前に永続化できる必要がある場合は、実際にはUser.DateOfBirth
はではありません(たとえば、EF CodeFirstではNOT NULLにすることはできませんその間にヌル値を保持できるようにする)。後で完全なモデルを検証するには、いくつかの条件付き検証(例:IValidatableObject、MvcFoolproof、Fluent Validation)を行う必要があります。
は個人的に、私は、各ウィザードステップのための別のビューモデルを使用したいと思います。これにより、必要な場所(つまり、現在のウィザードステップの値)で検証を使用することができます。 –