在 MVC 应用程序中重写数据验证的最佳方法是什么
我有一个非常大的表格需要用户填写。他们需要能够保存进度,无论是否有效。当他们稍后回来时,他们可以更新并使其有效 - 然后提交数据。
我在域模型上使用客户端脚本(xval)和服务器端属性。
I have a very large form I need the user to complete. They need to be able to save their progress whether valid or not. When they have come back at a later time, they can update and make valid - then submit the data.
I am using client side scripting (xval), server side attributes on the domain model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种选择是使用 [Save Draft] 按钮,该按钮将转到不同的 Action SaveDraft() 而不是 Save()。通过使用单独的方法,您可以决定保存操作所需的验证量。我认为这样更干净。
要使用相同的“保存”操作来决定是否应用一定数量的验证逻辑将更难以阅读和维护。
One option for you is to have a [Save Draft] button which would go to a different Action SaveDraft() instead of Save(). By having a separate method, you can decide on the amount of validation you want on the Save operation. That I think is cleaner.
To have the same Save action decide whether to apply the amount of validatation logic would be harder to read and maintain.
我对 xval 不太了解,但也许您可以考虑从 xval 属性继承并在那里进行检查...
例如 ConditionalRequiredAttribute :RequiredAttribute,它可能可以采用适当的状态进行验证。
如果满足验证条件(状态!=“草稿”),则验证将与 ConditionalRequiredAttribute 的 validate 方法上的 base.Validate() (或任何名称)一样简单。
由于我不知道 xval,所以我不确定如何客户端验证有效...它会创建客户端验证代码吗?在这种情况下,您必须编写在 javascript 中创建类似包装器的代码(例如 if(status != "Draft")...如果验证只是简单对服务器进行 Ajax 调用并返回错误列表,那么我想您不必执行任何其他操作。
抱歉,我无法直接回答您的问题,但这也许可以帮助您提出一些想法。 ..
I don't know much about xval, but maybe you can look into inheriting from xval attributes and doing the check there...
e.g. ConditionalRequiredAttribute : RequiredAttribute, which maybe can take the appropriate statuses to validate on.
Validation would be as simple as base.Validate() (or whatever it's called) on ConditionalRequiredAttribute's validate method if a validation condition is met (status != "Draft).
Since I don't know xval, I'm not sure how the client side validation works... does it create client side validation code? In this case, you'd have to write code that creates a similar wrapper in javascript (e.g. if(status != "Draft")... If validation simply makes an Ajax call to the server that returns a list of errors, then I imagine that you wouldn't have to do anything else.
Sorry I couldn't answer your question directly, but maybe this can help you come up with some ideas...