C# 服务器端表单验证和重新渲染
除了客户端之外,我希望能够在服务器端验证表单(也许检查数据库中是否有注册表单的重复条目)。我已经编写了验证逻辑,但我想重新呈现视图并使用户提交的表单值可用。另外,我想放置一条错误消息,并且可能更改字体的颜色或在字段无效的地方添加 X。基本上我希望做的事情与 formencode 和 htmlfill 在 Python 中所做的事情相同。在 C# 中执行此操作的最佳方法是什么?
I'd like to be able to validate forms server-side (maybe check the database for duplicate entries for a registration form) in addition to client side. I've written my validation logic, but I'd like to rerender the view and have the form values the user submitted to be available. Also, I'd like to place an error message and maybe change the color of the font or add an X where a field was invalid. Basically I'm looking to do the equivalent of what formencode and htmlfill does in Python. What would be the best way to do this in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 ASP.NET,最简单的方法是使用 CustomValidator 控件,您可以在验证器中放置自己的逻辑。您可以使用内置的验证器控件(例如RequiredFieldValidator 和CompareValidator)来完成许多验证。
Using ASP.NET, the easiest way would be using a CustomValidator control where you can place your own logic in the validator. A lot of the validation you could probably accomplish with the built-in validator controls like RequiredFieldValidator and CompareValidator.
您有多种选择:
(1) 使用 ASP.NET,您可以在服务器端进行回发并执行验证等。
(2) 将 Ajax 与 jQuery 或 Prototype 等 Javascript 库结合使用,您可以通过更清晰的用户体验来完成同样的事情。
You have a copule of options:
(1) Using ASP.NET you can do a Postback and perform your validation, etc. on the server-side.
(2) Using Ajax with a Javascript library such as jQuery or Prototype you can accomplish the same thing with a cleaner user experience.