如果我使用 TryUpdateModel,如何清空集合?
我有一个包含 List
属性的模型。我在更新操作中使用 TryUpdateModel。添加新号码和更改现有号码效果很好。然而,只有当我不尝试删除所有内容时,删除现有号码才有效。如果我从列表中删除所有内容,则不会删除任何项目。
我意识到这可能是设计使然,但是处理这个问题的推荐方法是什么?
I have a model that contains a List<PhoneNumber>
property. I use TryUpdateModel in my update actions. Adding new numbers and changing existing numbers works fine. Removing existing numbers, however, works only if I don't try to remove everything. If I remove everything from the list, none of the items get deleted.
I realize that this is probably by design, but what's the recommended approach for dealing with this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我目前正在采用这种方法:
I'm currently going with this approach:
我知道这个问题已经有 2 年历史了,而且是针对 MVC2 的。然而,我确实在MVC3中发现了同样的问题,并找到了罪魁祸首和解决方案。我回答了以下问题,因为这与我遇到的问题相同,希望该解决方案也适用于此处,无需更改。
相关答案:当屏幕上的所有项目被删除时,TryUpdateModel 不会清空项目集合。
I know the question is 2 years old and for MVC2. However, I did find the same issue in MVC3 and found the culprit and the solution. I answered the following question since it was the same problem that I was having, and hopefully, the solution is also applicable here without changes.
Related answer: TryUpdateModel does not empty a collection of items when all items are removed on screen.