如何在ASP.NET MVC 2的验证实际工作? [英] How does validation in ASP.NET MVC 2 actually work?

查看:140
本文介绍了如何在ASP.NET MVC 2的验证实际工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过追溯为什么我的ASP.NET MVC 2验证不工作,但我不能找到足够的了解它是如何工作才能够做到这一点。

I am trying to trace through why my ASP.NET MVC 2 validation isn't working, but I cant find enough about HOW it works to be able to do this.

我跟了由大卫·海登在这个有用的文章的步骤似乎是最好的文档目前摆在那里,但没有实际发生。

I have followed the steps in this useful article by David Hayden which seems to be the best documentation currently out there, but nothing actually happens.

我得到验证,当我提交给服务器(如我因为preVIEW 1,当我添加的数据注解来我的模型做了),但我没有得到任何客户端验证。

I get validation when i submit to the server (as I did since Preview 1 when i added data annotations to my model) but I'm not getting any client side validation.

如何跟踪通过测试?到目前为止,我已经验证了以下明显的事情

How can i trace through to test? So far I have verified the following obvious things


  • MicrosoftMvcJQueryValidation.js和jquery.validate.min.js文件被下载

  • Html.ClientValidationEnabled = TRUE

我不能很容易地看到是什么挂钩事件颇知道如何调试它。

I cant see easily what is hooking up to which events to know quite how to debug it.

推荐答案

下面是我学到的东西:

最重要


  • 您的HTML表单必须与使用指令,不只是BeginForm和端部结构进行创建。

  • 您必须设置Html.ClientValidationEnabled =真在你开始你'形式'

  • 您必须使用Html.ValidationMessage为每个字段

  • 您必须设置Html.ClientValidationEnabled =各部分控制真(ASCX)

如何工作(非常简单的概述)

HOW IT WORKS (very simple overview)


  • 当你做Html.BeginForm它创建了一个FormContext在ViewContext

  • 当使用ValidationMessage帮手,元数据放入表格背景

  • 在当前的形式布置(由using语句),它写出所有验证code

MISC


  • 我似乎无法得到验证工作时,我有一个部分控制,如果控制使用不同的模型从定义表单视图。

  • I cannot seem to get validation working when I have a partial control, if that control uses a different model from the view that defines the Form.

您不需要使用Html.TextBoxFor或Html.ValidationMessageFor,您可以使用Html.TextBox和Html.ValidationMessage

You do NOT need to use Html.TextBoxFor or Html.ValidationMessageFor, you can use Html.TextBox and Html.ValidationMessage

这篇关于如何在ASP.NET MVC 2的验证实际工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆