asp.net 4.5 webforms模型绑定:支持客户端验证吗? [英] asp.net 4.5 webforms model binding : client side validation supported?

查看:90
本文介绍了asp.net 4.5 webforms模型绑定:支持客户端验证吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用数据注释的asp.net 4.5 Webforms模型绑定的忠实拥护者.

I'm a huge fan of asp.net 4.5 webforms model binding using data annotations.

ascx:

     <asp:FormView ItemType="Contact" runat="server" DefaultMode="Edit" 
     SelectMethod="GetContact" UpdateMethod="SaveContact">
        <EditItemTemplate>   

              <asp:ValidationSummary runat="server" ID="valSum" />

              Firstname: <asp:TextBox  runat="server"  ID="txtFirstname" Text='<%#: BindItem.Firstname %>' /> 


              Lastname: <asp:TextBox  runat="server"  ID="txtLastname" Text='<%#: BindItem.Lastname %>' />

              Email:  <asp:TextBox  runat="server"  ID="txtEmail" Text='<%#: BindItem.Email %>' />     

              <asp:Button ID="Button1"  runat="server" Text="Save" CommandName="Update" />
        </EditItemTemplate>   
    </asp:FormView>

.cs:

    public void SaveContact(Contact viewModel)
    {
        if (!Page.ModelState.IsValid)
        {
            return;
        }            
    }              

    public Contact GetContact() 
    {
         return new Contact();
    }

型号:

    public class Contact
    {
        [Required]
        [StringLength(10, ErrorMessage="{1} tis te lang")]   
        public string Firstname { get; set; }

        [Required]
        [StringLength(10)]
        public string Lastname { get; set; }

        [Required]
        [EmailAddress]       
        public string Email { get; set; }

    }

问题:

像MVC一样,Web表单中是否支持开箱即用的客户端验证? 还是我们应该依赖第三方库(DAValidation).是否可以将Html.EnableClientValidation()的优点移植到Webforms上?

Is client side validation supported out-of-the-box in webforms like in MVC? Or should we rely on third party libraries (DAValidation). Is it possible to port the goodness of Html.EnableClientValidation() to webforms ?

此致

巴特

推荐答案

正如我们在ASP.NET WebForms项目中发现的那样,对于客户端验证,没有整体有用地重用Model的验证属性.

As we have found in our ASP.NET WebForms projects, there is no overall useful reuse of the Model's validation attributes for client side validation.

例如,具有各种属性(例如姓名,电子邮件,生日等)的联系人数据模型并非总是以相同的方式使用.有时它可能有一些必填字段,有时没有,甚至在应用程序中的各个点所需的输入数据也可能有所不同.

For example, a contact data model, with various properties like name, email, birthday etc... is not always used the same way. Sometimes it may have some mandatory fields, sometimes not, and even the required input data may differ at various points in the application.

因此,在我们的项目中,我们同时使用了客户端验证实现和模型属性.

Thus, in our projects, we use both a client side validation implementation, and the model attributes.

我们采用的总体思路是:

The general idea we apply is:

  • 在客户端,我们希望尽可能具体,以避免不必要的回发,并为用户提供即时,具体的响应.
  • 在服务器端,我们应用了模型属性以及更多的数据库和面向业务的验证规则,但并非如此.另外,如果需要,当某些字段相互依赖时,会进行一些属性间"验证.

对于客户端,我们选择了jQuery Validate插件( http://jqueryvalidation.org/).

For the client side, we have chosen the jQuery Validate Plugin (http://jqueryvalidation.org/).

我们甚至已经构建了自己的控件集(这些控件来自内置的WebControls),它们可以呈现各种(甚至是一些自定义)数据规则.

We even have built our own set of controls (that derive from the built-in WebControls), which render various (and even some custom) data rules.

这篇关于asp.net 4.5 webforms模型绑定:支持客户端验证吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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