ASP.NET MVC 2 Preview 2:Client-Side Validation

ASP.NET MVC 2 Preview 2: Client-Side Validation

In my opinion one of the most interesting features of ASP.NET MVC 2.0 Preview 2 is the support for client-side validation.

How-to enable Client-Side validation

Client-side validation enable your application to perform. client-side validation based on model’s validation by JQuery validation library. Ok, it’s simpler than it sounds.

The client-side validation framework is even extensible, but for now it suits my simple needs.

In my post on ASP.NET MVC 2.0 Preview 1 I’ve added server-side validation to my application model using metadata as ASP.NET Dynamic Data does. So, for example, in the the picture below I’ve added a partial class to my model and a class for metadata describing my requirements for validation. In the following code the attribute ‘Titolo’ must be not null, and I’ve added an Error Message if something’s wrong.9914854_201007021603081.jpg

If I run the application all works fine: I show a view page to the user, user enters some value and press Save to submit to server. If the user doesn’t fill the title field he gets an error as soon as we made a POST to server. You can see the page flickering in the browser.

Client-side validation prevent your app going to the server for validation and all happens client side, but using the rules you have defined on the partial class you saw before.

To enable client side validation, you can follow these easy steps:

· use Html.EnableClientValidation method on the View file

· insert three javascript. files : jquery-1.3.2.js, jquery.validate.js, MicrosoftMvcJQueryValidation.js. These files are included in the Scripts directory coming with ASP.NET MVC 2 Preview 2 project template. 

I’ve put the code in my view page for editing some simple fields.9914854_201007021603082.jpg

Now I can run my application and see the results, I’m using HttpWatch to see that no requests are performed to the server and the validation logic works as expected.9914854_201007021603083.jpg

As soon as I type something into the textbox the red message disappears.

Nice feature! You can download the ASP.NET MVC 2 Preview 2 from here and read about all the features on Phil Haack post.

 

新的ASP.NET MVC 2 -预览1

Dato che oggi è stato rilasciata la preview 1 di ASP.NET MVC 2 ho deciso di aggiornare il progetto che avevo usato a Remix09 alla nuova versione per esplorare un po' delle nuove funzionalità.因为今天被释放的ASP.NET MVC 2预览1,我决定升级项目我以前Remix09新版本探索出的新功能位' (ASP.NET MVC 1.0 e ASP.NET MVC 2 possono coesistere side-by-side senza problemi) ASP.NET 1.0ASP.NET MVCMVC2可以共存并排而任何一方的问题)

Nella Release Note è documentato come aggiornare manunalmente il progetto, modificando gli assembly referenziati e il web.config.在发行说明文件manunalmente如何更新项目,修改程序集引用和Web.config

Nel mio caso, dopo aver scaricato ed installato i bit ho creato un progetto con il nuovo template per Visual Studio 2008 SP1, che vedete in figura :就我而言,后下载并安装了我创建了一个新模板的Visual Studio 2008 SP1的项目位,你在图片中看到: 9914854_201007021603084.jpg

Quindi ho fatto un semplice taglia e copia dei folder che mi intersessavano, più qualche modifica:所以,我犯了一个简单的剪切和复制的文件夹,我intersessavano,另外还有一些变化:

  • AppData: che conteneva il database delle sessioni应用程序数据:含有数据库会话
  • il file remix09.css che ho messo nel folder Content remix09.css我把文件夹中的内容文件
  • il mio controller HomeController.cs我的控制器HomeController.cs
  • Tutto il contenuto della cartella Model, che contiene i file per implementare il Repository per le sessioni ISessionsRepository.cs, la classe repository SessioniRepository.cs ed il file di Entity Framework per le cassi del mio modello您的模型,其中包含文件来实现会议的ISessionsRepository.cs库的全部内容,班级库SessioniRepository.cs和文件实体框架来对我的模型的库房
  • ho aggiunto la stringa di connessione al mio web.config我添加了连接字符串以我的web.config
  • Ho sostituito il folder Views/Home con le view che avevo fatto余取代了我这样做了的文件夹视图/首页
  • Ho sostituito la pagina Views/SharedViews/Site.Master con quella usata nel vecchio progetto余取代了页面浏览量/ SharedViews / Site.master所使用的旧的项目
  • ho fatto il find & replace del namespace che usavo nel precedente progetto con il nuovo namespace, che nel mio caso si chiama Remix09Mvc2preview1 (il nome che ho dato al progetto).我的确发现和替换的命名空间,我在与新的命名空间,这对我来说,以前被称为Remix09Mvc2preview1项目中使用(这个名字我向项目)。 Attenzione alle views che se strongly-typed, come nel mio caso, contengono il riferimento al nome completo delle classi.注意事项的意见,如果强类型,因为在我的情况,包括对完整的类名称引用。
  • ho aggiunto nella sezione assemblies, nel web.config, il riferimento, prendendolo dal web.我在web.config节集会补充说,参考,从网络到它。 config del progetto vecchio, a System.Data.Entity che viene usato da Entity Framework配置旧设计的,它是由System.Data.Entity实体框架使用

Ed ecco la mia demo funzionare come prima :这里是我的工作才演示: 9914854_201007021603085.jpg

DataAnnotation: validazione DataAnnotation:验证

Una delle novità riguarda l'utilizzo del meccanismo delle DataAnnotation, già usato dai Dynamic Data e nei RIA Services, con cui posso aggiungere degli  attributi alla mia classe del Modello, nel mio caso ho un'unica classe che descrive una “Sessione”, dato che l'intera applicazione consente di gestire le sessione dell'evento Remix.一个改变涉及的DataAnnotation机制的使用,已被俄动态数据和服务,用于我可以将属性添加到我的模型类在我的情况,我有一个类,它描述了一个会议,因为整个应用程序允许您管理活动会议不怕。

Nella demo di Remix, potevo creare una nuova sessione, ma non veniva verificato se, ad esempio titolo e sessione fossero valori diversi da null.在演示的不怕,我可以创建一个新的会话,但没有查证,例如标题和本届会议的非NULL值。

Con ASP.NET MVC 2 ho un reference già aggiunto, come vedete dalla figura:ASP.NET MVC 2我已经添加了一个参考,正如你在图中看到: 9914854_201007021603086.jpg

Quindi, come con i Dynamic Data, posso scrivere una classe parziale, che andrò ad aggiungere al Model e che mi dà controllo server-side sulle proprietà della mia classe:因此,当与动态数据,我可以写一个局部类,我将添加的标准,使我对我的类的属性控制服务器端: 9914854_201007021603087.jpg

Ho creato una classe SessioneMetadata nella quale posso specificare delle regole di validazione per le proprietà della classe Sessione.我创建了一个类SessioneMetadata在我指定的类会议属性验证规则。 In questo caso l'attributo Required ed il Relativo ErrorMessage fanno si che se inserisco una sessione vuota abbia degli errori di validazione, per ora (:-) ) server-side, come vedete dalla figura, dove ho usato la stessa View, senza nessuna modifica, che ho usato con la versione 1.0 di ASP.NET MVC.在这种情况下所需的属性及其出错提示的意思是,如果我进入一个空会话已验证错误,现在(:-))服务器端,你可以看到这个数字,在那里我用同样的看法,没有,变化,我与版本的ASP.NET MVC 1.0使用。

 

fj.png1.JPG

fj.png2.JPG

fj.png3.JPG

fj.png4.JPG

fj.png5.JPG

fj.png6.JPG

fj.png7.JPG

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9914854/viewspace-667010/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9914854/viewspace-667010/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值