Wednesday, December 7, 2011

Client-Side Model-Aware Validation in MVC3

Just a note so mostly, I remember.

I've been working with Nick Riggs' Foolproof validation in MVC3, trying to get more complex scenarios in place, and needed to write the client validation.  He has a great post for MVC2 at
http://www.nickriggs.com/posts/client-side-model-aware-validation/, but it doesn't cover MVC3.

After some research, etc., here is what I came up with for something very simplistic, where it's just making sure a value is set, etc...

$(function () {

    jQuery.validator.addMethod('listrequirediftrue', function (value, element, param) {
        if (param) {
            return $('#' + $(element).attr('id') + ' option:selected').length > 0;
        }

        return true;
    }, '');
    jQuery.validator.unobtrusive.adapters.add('listrequirediftrue', {}, function (options) {

        if (options.message) {
            options.rules['listrequirediftrue'] = true;
            options.messages['listrequirediftrue'] = options.message;
        }

    });
} (jQuery));

I just need to now figure out for more complex scenarios, like when you have a RequiredIf, but dependent on when another dropdown has a certain value.  Nick has the RequiredIfAttribute, but when you're validating based on an entire model, where the property to be validated is not at the same level as the property whose value is the "RequiredIf" property, you have to use the ModelAware style of validation... will update when I have an answer.

Thursday, November 3, 2011

More quirks with ASP.NET MVC 3

So, another great quirk with ASP.NET MVC 3.  When you implement a controller with two methods, one being for the initial load of the page, and the other being for the subsequent form post to process the data... if your intention is to re-render the page to do another 'Add Item' scenario, you must issue a ModelState.Clear(); command before you render the new view... otherwise, it seems to keep the values from the posted data.

Monday, June 27, 2011

ASP.NET MVC3 has a few quirks

http://aspnet.codeplex.com/workitem/7629

Ack!  Seems like this shouldn't have happened. 

To correct, you have to do the following:

Give your select list the "Required" attribute.
Once the page loads, in jQuery, assign the data-val attribute and the data-val-message attributes.

Microsoft, this is not good!

Tuesday, June 7, 2011

Team Foundation Server 2010 isn't friendly for certain items...

It seems as if this is a normally recurring issue with build automation and Team Foundation Server 2010 specifically.  Utilizing Team Foundation Server's build automation, it takes the *.sln and *.csproj files and utilizes that to create the output binaries as expected.  What is unfortunate is that the .Net SDK doesn't have everything that you need to handle build automation on a build server, so you're forced to do one of 2 things:
  1. Deploy VS.Net 2010 on your build server(s)
  2. Copy the files from c:\{Program Files x86}\MSBuild\Microsoft\VisualStudio\... to your build machine.
Honestly, both solutions break the fundamental rules of using a build automation server, IMHO.  Completely disappointed Microsoft! I think that I'll be writing ScottGU or someone else to see if they have a better solution to handle this case, as it's simply a poor "workaround" solution.

Thursday, June 2, 2011

Textarea Autoexpand Javascript code.

As part of a project for a consulting job I'm on, the need we had was to give a forever expanding text area on the html form.  This is the javascript I wrote to do just that.  I'm sharing it because mainly, I think it's cool!

I should have documented how this works. What you'll do in the Html is to define a text area (could be an asp TextBox with its mode set to Multiple, or a generic TextArea tag... either way, they render out the same.) and you'll add an attribute called expandable and set its value equal to True.  From there, reference jQuery in your header, and this code should work for you as-is.

Have at it, and comment if you see where I could improve in some way.

          function formatPage() {
            $(document).find('*[expandable]').keyup(function () {
                var height = 0;
                var existingHeight = this.style.height;
                var lines = this.value.split('\r');
                this.style.height = '';
                for (x = 0; x < lines.length; x++) {
                    var lengthOverflow = lines[x].length % this.cols;

                    height = height + (14 * (((lines[x].length - lengthOverflow) / this.cols)));
                    if (lengthOverflow > 0) {
                        height = height + 14;
                    }
                }

                if (height == 0) {
                    height = 14;
                }

                if (existingHeight != height) {
                    this.style.height = height + 'px';
                }
            });

            $(document).find('*[expandable]').keyup();
        }

        $(document).ready(function () {
            formatPage();
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
        });


        function endRequestHandler(sender, args) {
            formatPage();
        }