Tag Archive | MVC

Localization validation in MVC

Last week I was working on a bug for my website SeeTings. The bug was the validation of a DateTime not working in Firefox and Chrome. In Internet Explorer (even version 9) was working correct.

After hours searching and hundreds of form posts further, I finally found the solution. I thought the problem was in implementing the jQuery UI Date picker with the Dutch Format of a date (dd-MM-yyyy) in it but that wasn’t true.

The problem was the client validation of the jQuery Validation plugin. ASP .NET MVC 3 and 4 are shipped with an installed version of jQuery. And the internet template (the one that I was using) creates a login and register process.

To test the bug, I created a new project and added a DateTime field called Birthday to the register login form. I deleted all other fields except the UserName field because then I could test if the Required validation still worked if I might fined a solution for the validation problem.

After long searching, I saw this post http://stackoverflow.com/a/511670 on Stackoverflow. There was another script file called “additional-methods.js” that I didn’t heard of. After long searching, I found the official website of jQuery validation http://bassistance.de/jquery-plugins/jquery-plugin-validation/ and saw the CDN of all the jQuery instances.

It seemed not necessary to link the additional-methods.js file because that would only add extra validation like credit card or url TLD and email TLD etc. checking.  I found out that jQuery validation had multiple localization files. One for the messages and one for one other thing and that was my problem, the date validation.

When you go to the CDN of Microsoft, you will see in version 1.6 (current version is 1.9) a long list of localization files.

imageimage

The files ending with messages_xxx.js are the error messages localized. The files ending with methods_xx.js (like for me methods_nl.js) are the localized versions of some script. In this case only the date.

So reference on your page where you need to do the validation, the standard validation scripts of jQuery and after that you need to reference you localization file. Only do this if the browser is that language. So I only reference the script file if I know that the browser is Dutch. You can find the current language settings of the browser with JavaScript as well. You can change the url of the CDN from 1.6 to 1.9 and it will still work. Why the files are not listed on the 1.9 page, I don’t know.

After completing this above, you can after that implementing the jQuery Datepicker. Don’t forget that the Datepicker of jQuery UI also needs a localization reference file. See the last part of the documentation on this page http://jqueryui.com/demos/datepicker/. Just reference the file and everything is working.

Good luck and hopefully you guys aren’t that long busy figuring out why the validation isn’t working.

Advertisements

Minification and Bundling in MVC 4 RC

Asp .NET MVC 4 Release Candidate is out. One of the features that is included is called the minification and bundling feature. This was already there in the beta but in the release candidate version it has changed.

Why should we use it

The reason why we should use the bundling and minification feature is performance. The features increases your loading performance of your website. Every time you reference a JavaScript (like jQuery or your own), or CSS file in your page, the browser makes a call to the server. This is done for each separate reference. Each referenced file has included all the comments and spacing in your file. This makes the file larger then when we should delete those spaced. The bundling and minification feature does this for us.

How does it work

In your Global.asax the CSS and JavaScript files are Bundled with BundleConfig.RegisterBundles(BundleTable.Bundles); line.

image

image

Reference the files in your page.

image

When you run the application and use Fiddler to view the calls to your server, you still see all the files called separately.

image

image

This is because the Bundling and Minification feature by default only work when your not in debug mode. This handy because then you could debug with all the whitespaces in your files and have the performance in the production environment.

See the difference in your production environment:

image

image

Force Bundling and Minification

You can use the BundleTable.EnableOptmizations override but the best way for a little test is to remove the debug=”true” attribute in your web.config.

image

Browser caching

When the feature is active, the browser will cache the files. When you add or change some JavaScript or CSS code, the files are generated again and the version number in the references are updated. In that way, the browser knows that there is a new version and your website wont brake.

image

HTML klaar voor het grote werk?

Als ik kijk naar de volwassenheid van het huidige web is HTML zelf nog niet helemaal klaar voor het grote werk. Als ik kijk naar HTML 5 in combinatie met frameworks als JQuery, Modernizr etc. zeg ik dat het de goede kant op gaat. De laatste versies van alle browsers werken goed maar er zijn nog steeds een hoop verschillen in functionaliteit die worden ondersteund. Microsoft zegt dat IE9 helemaal het einde is maar ze weten dat ze snel achter zullen lopen. Microsoft zegt zelf dat er geen release cyclus van +/- 6 weken komt voor IE maar ondertussen is de eerste versie van IE10 al in zicht. Ze zullen wel moeten want de concurrenten gaan veel sneller. Ik denk dat we op een gegeven moment ook alleen maar horen over IE en niet meer IE x. Weet iemand uit zijn hoofd wat de laatste versie van Chrome ondertussen is? Pssst antwoord is 11 sinds deze week.

Alle features van HTML 5 zijn nog lang niet bekend. Dit kan nog wel een tijd gaan duren. Tot die tijd zullen we moeten checken op features door middel van frameworks. Als ik verder kijk dan alleen de computer zal HTML bij mij toch echt de voorkeur krijgen. De tablet oorlog is volgens de analisten pas dit jaar van start gegaan. Het aantal tablets bij de consumenten en bedrijven (ik hoor het steeds meer om me heen bij bedrijven) groeit met een rap tempo. Het aanhouden van een standaard is dan wel zo makkelijk.

Ontwikkelmethodieken als MVC stellen je in staat logica en presentatie makkelijker te scheiden. Het compatible maken/ondersteunen van je applicatie voor verschillende platformen is dus ook een stuk eenvoudiger. Je bereikt hierdoor een groter publiek en zal dus meer omzet genereren. De techniek staat niet stil waardoor we onze strategieën zelf moeten beslissen maar wat je wel ziet is dat de doelgroep of manier van werken langzaam wijzigt. De IPad 2 is uit en meteen heel het internet staat vol met tweede hands IPad 1 omdat die oud is. Er is veel meer vraag naar nieuwe techniek en alle informatie dient voor alle gadgets en platformen aanwezig te zijn. Mensen verwachten tegenwoordig dat dit ook zo wordt gemaakt.

De smartphoneverkoop neemt in een jaar tijd met 83% toe. Het aantal verschillende telefoons op de markt met verschillende specificaties neemt ook rap toe. Het gebruiken van standaarden begint nu dus echt belangrijk te worden. Grote organisaties als Microsoft, Google en Apple omarmen dit ook. Het is niet voor niets dat grote organisaties steeds meer open source projecten financieel ondersteunt zodat hier de community weer gebruik van kan maken. Soms worden projecten door dit wel is stop gezet. Denk maar aan de AJAX techniek van Microsoft. Microsoft ontwikkelt zelf nu voor JQeury. Heel het templating in JQuery komt namelijk van Microsoft.

Default browser VS2010 Add-in

Ralph JansenLast weekend I was working on a MVC 2 application. When I started the application my default browser (Google Chrome) came up. I actually wanted to change my browser for this application to Internet Explorer because the connectivity between IE and Visual Studio 2010 is much better than with Chrome.

The problem is that you can’t choose for the option “Browse With” in a MVC Application. This is because the controller is deciding which view should start. To fix this problem, I found an add-in for VS2010 that you can switch your default browser on the fly from your toolbar. In that way, you can switch easier between your installed browsers. See the screenshot below. image

You can download the add-in by following the link below.
http://visualstudiogallery.msdn.microsoft.com/en-us/bb424812-f742-41ef-974a-cdac607df921

 

Update:
If you get a popup on the startup of your Visual Studio Instance with the text: “The bits have expired, please download an updated version from http://www.wovs.com”, you have to download the new version. To do this, follow the steps on this website: http://blog.wovs.com/2010/10/how-to-update-currently-expired.html
Search in the online extension manager for “Default browser switcher”.

ASP .NET MVC 3 Release Candidate released

Ralph JansenOn November 9th 2010 the ASP .NET team released the release candidate of MVC 3. The big change of MVC 3 is the new “Razor” view engine. With this new view engine the syntax of the code is a lot smaller. You now just put an @ on your editor and the intellisense will pick it up.

An other thing that is added in MVC 3 is: partial caching of pages. You can add an attribute above your action in the controller with the right amount of seconds that you want to cache the information. You can even say what the dependent parameter is that the caching will focus on. So if an action is fired and the parametervalue is the same as a previous parametervalue within the specified amount of time, the data will come out of the cache. That way, no database call is required!!!

Check for more information the blog of Scott Guthrie or the official ASP .NET MVC 3 Releas Candidate page.