我将一个更大的模型推送到一个视图,但只想更新该视图的一部分,该视图具有列表的多个部分视图。
本质上,我有更新原始模型的代码,但希望 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 ”运行验证。
我的原始信息更新正常,但需要对原始信息而不是 sectionUpdates 运行验证。好像已经有一个 a.Prop1,在帖子的视图中没有输入字段。它比较大,并且不想在不需要的情况下将大量隐藏字段发布回服务器。