より大きなモデルをビューにプッシュしていますが、リストの複数の部分ビューを持つビューの一部のみを更新したいと考えています。
基本的に、元のモデルを更新するコードがありますが、投稿された部分ではなく、更新された元のモデルに対してModelState.IsValidを動作させたいと考えています。
[HttpPost]
public virtual ActionResult MyAction(MyFullModel sectionUpdates)
{
var updated = Session['original'] as MyFullModel;
for (var i=0; i<updated.Section.Count; i++)
{
var a = original.Section[i] as SubModel;
var b = sectionUpdates.Section[i] as SubModel;
if (String.IsNullOrWhiteSpace(a.Prop1))
{
a.Prop1 = b.Prop1
}
if (String.IsNullOrWhiteSpace(a.Prop2))
{
a.Prop2 = b.Prop2
}
...
}
// ??? How do I run ModelState.IsValid against original here ???
// this doesn't seem to work, the only the posted values are checked...
// ViewData.Model = model;
// ModelState.Clear();
// if (!TryUpdateModel(model))
// {
// //model state is invalid
// return View(secureFlightUpdates);
// }
}
上記の「 sectionUpdates 」ではなく「updated 」に対して検証を実行したい。
元の情報は正常に更新されていますが、sectionUpdatesではなく、元の情報に対して検証を実行する必要があります。すでにa.Prop1が存在するかのように、投稿のビューに入力フィールドがありません。それは比較的大きく、大量の隠しフィールドを必要なしにサーバーに送り返したくありません。