如果 ModelState 表明字段无效,则清除字段值
如果 ModelState 显示字段无效,我想清除模型中字段的提交值。
这是我到目前为止所得到的,但无法将价值的关键与模型联系起来。有什么建议吗?
if (!ModelState.IsValid)
{
foreach (string key in ModelState.Keys)
{
if (!ModelState.IsValidField(key))
{
// This field is not valid so set to empty string in model
// Something like....
model[key] = "";
}
}
}
I want to clear the submitted value of a field in a model if the ModelState
shows that the field is not valid.
This is where I have got so far but can't tie up the key to value in the model. Any suggestions?
if (!ModelState.IsValid)
{
foreach (string key in ModelState.Keys)
{
if (!ModelState.IsValidField(key))
{
// This field is not valid so set to empty string in model
// Something like....
model[key] = "";
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该返回与收到的模型相同的视图,并将代码更改为以下内容:
You should return the same view with the received model and also change your code to the following: