Saturday, December 31, 2011

Short comment on UnobtrusiveJavaScriptEnabled app setting

While looking into web.config file of the ASP.NET MVC 3 application you would notice an application setting called UnobtrusiveJavaScriptEnabled. Not sure what about you but for me it looked like a mysterious setting for turning on/off some mysterious functionality.

The purpose of that setting is explained by Brad Wilson in his post Unobtrusive Client Validation in ASP.NET MVC 3. Putting it short - with this setting turned off client side validation is being performed using Microsoft javascript libraries (the same way it was performed in ASP.NET MVC 1 and 2). Otherwise (with setting turned on) client side validation is performed using JQuery Validate.

Friday, December 30, 2011

Implementing Cancel button functionality in ASP.NET MVC 3

Implementing a Cancel button functionality - currently I cannot imagine a better topic for my first blog post ever. And no matter how trivial the topic is, I am sure there is plenty of room left for me to slip somewhere.

I am a huge fan of ASP.NET Web Forms framework. I love the stateful Web model approach and yes – I am not afraid of postbacks, I am using viewstate and I think I could not live without server side controls and code-behind in my web pages.

And now I am trying to fall in love again with ASP.NET MVC. Which is not an easy task for me, but I am moving on.

While playing around with my ASP.NET MVC application I came across a situation where I needed to implement a simple data entry form. It had some text fields on it, a Save button to submit entered information and a Cancel button which would just redirect to an index page. The problem for me was – how to implement cancel button functionality?

One of the possible solutions is presented by Andrey Shchekin (Multiple submit buttons with ASP.NET MVC: final solution). This could work, but I don’t want to have the complete MVC page life cycle being triggered just for a cancel button functionality. More than that - I don’t even want to make Cancel button as a submit button because I don’t want validation of my form to be triggered while clicking it.

Solution I found suitable for me is simple: 

<input type="submit" value="Save" />
<input type="button" value="Cancel" 
       onclick="javascript:document.location.href='@Url.Action("Index", "Home")'" />