gpt4 book ai didi

asp.net-mvc - 发布表单数据后如何使用 ModelState 清除表单,我的 View 模型是一个具有两个模型的 View 模型?

转载 作者:行者123 更新时间:2023-12-03 07:56:42 25 4
gpt4 key购买 nike

我的 View 模型是一个有两个模型的 View 模型,

  publish class ViewModel
{
public Student StudentModel{get;set;}
public Teacher TeacherModel {get;set;}
}

我有两个表单,分别是StudentForm和TeacherForm,分别绑定(bind)了StudentModel和TeacherModel。现在我在StudentForm中填写一些数据并提交。我用ModelState.Clear();来清空我的StudentForm数据,但是好像不仅清空了StudentForm的数据,连TeacherForm的数据都清空了,怎么只清空StudentForm的数据呢?

最佳答案

为了缩短,你可以这样做

foreach (var key in ModelState.Keys.Where(m => m.StartsWith("StudentModel")).ToList())
ModelState.Remove(key);

关于asp.net-mvc - 发布表单数据后如何使用 ModelState 清除表单,我的 View 模型是一个具有两个模型的 View 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18661401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com