Skip to content

Instantly share code, notes, and snippets.

View danielgreen's full-sized avatar

Daniel Green danielgreen

  • Glasgow, United Kingdom
View GitHub Profile
@danielgreen
danielgreen / ValidateHiddenFields.js
Last active October 26, 2023 14:19
The jQuery Validation plugin does not validate hidden fields by default. Here is how to get around that.
// By default, the jQuery Validation plugin ignores hidden fields. You may want them to be validated however.
// The default 'ignore' setting is ':hidden'
// See https://github.com/jzaefferer/jquery-validation/issues/189
// Attach a validator to a particular form, with a blank 'ignore' setting.
// If you are using Unobtrusive Validation then you can't do this, as the Unobtrusive script initialises the plugin for you.
// Instead, you must either amend the setting on the validator once it's attached to the form,
// or amend the default setting before the validator is attached (read on to see how).
$("MyForm").validate({ ignore: "" });
@danielgreen
danielgreen / GarberIrish.js
Created May 30, 2013 11:30
Garber-Irish JavaScript implementation. Provides a way to execute script, on page load, based on the MVC controller and action that produced the page. This is a DOM-routing approach. It can help pages to avoid explicitly referencing numerous JS files. See http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution
/* Contains general scripts that may be used in any page.
* If this file starts to get large it can be split into page-specific files. */
/* The following code is the Garber-Irish implementation, a way to run relevant JavaScript on page-load
* based on the MVC action that produced the page. It's an unobtrusive approach, which means that the
* code to call the relevant JavaScript functions is all here instead of being hardcoded into the HTML.
* All this code needs from the page is data-controller and data-action attributes on the body tag.
* Since JavaScript is case-sensitive, the controller and action names we use here must be an exact match.
* http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution */
@danielgreen
danielgreen / BaseConfigurationElementCollection.cs
Created June 13, 2013 10:43
Code to create a custom configuration section with child collections
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Config
{
public abstract class BaseConfigurationElementCollection<TKey, TElement>
@danielgreen
danielgreen / ParseJSONDates.js
Last active March 8, 2021 22:26
When an ASP.NET MVC 4 action returns JSON containing a DateTime value, the value is serialized into a format that cannot be natively understood by JavaScript. Here is some code to process the value into a JavaScript Date. A better solution than this is to use JSON.NET to serialize dates instead. See https://gist.github.com/danielgreen/5669903
function dateTimeReviver (key, value) {
var a;
if (typeof value === 'string') {
a = /\/Date\((\d*)\)\//.exec(value);
if (a) {
return new Date(+a[1]);
}
}
return value;
}
@danielgreen
danielgreen / invalidHandler.UnobtrusiveValidation.js
Created May 29, 2013 14:02
When using jQuery Validate, you may want to run some code when it detects that the form is invalid. When using Unobtrusive Validation, you don't have the ability to set invalidHandler directly when initialising the Validate plugin, so here is a workaround.
// If using Unobtrusive Validation then that library initialised the Validate plugin
// on our behalf, so we missed the chance to set invalidHandler. Even if we were to
// set invalidHandler now via the settings object it would have no effect, due to the
// way that Validate works internally. Instead, we can do the following:
$("#MyForm").bind("invalid-form.validate", function () {
// Do something useful e.g. display the Validation Summary in a popup dialog
$("div.ValSummary").dialog({
title: "Information",
modal: true,
resizable: false,
@danielgreen
danielgreen / AjaxResultAttribute.cs
Last active January 15, 2018 08:36
ASP.NET MVC does not help you to display validation errors, perform browser redirects, or update the UI if the browser postback used Ajax instead of a normal form post. The following code deals with these situations using Knockout. Use ajax_ProcessSuccess as the OnSuccess callback for Ajax.BeginForm (see http://bradwilson.typepad.com/blog/2010/1…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Web.Json;
namespace Web.Filters
@danielgreen
danielgreen / CheckApplicationOfflineAttribute.cs
Created May 29, 2013 15:49
A filter (which should be used as a global filter) to check whether the application is configured as offline, and throw an exception if this is the case. This is an alternative to placing a file named App_Offline.htm in the application's root. Using App_Offline.htm affects all users, cannot be selectively bypassed as in this case, and does not a…
using System;
using System.Net;
using System.Web.Mvc;
using System.Web.Routing;
namespace Web.Filters
{
/// <summary>
/// Check whether the application is marked as offline in the config file.
/// If so, and the user does not have the override permission, throw an exception.
@danielgreen
danielgreen / Bootstrapper.cs
Created June 4, 2013 14:28
Using Unity in MVC - setting up Dependency Injection for your application. See http://www.devtrends.co.uk/blog/integrating-the-unity.mvc3-1.1-nuget-package-from-scratch
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Unity.Mvc4;
namespace MvcApplication1
{
public static class Bootstrapper
{
public static void Initialise()
{
@danielgreen
danielgreen / AppContainer.cs
Last active May 27, 2016 06:24
Suggested Unit Of Work facade to wrap around the Entity Framework ObjectContext. May require adjustment for use with DbContext in latest versions on Entity Framework. Includes a mechanism to perform concurrency checks on entities based on timestamps.
// http://dillieodigital.wordpress.com/2011/10/12/automatic-user-and-time-stamping-in-entity-framework-4/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Web;
using MyApp.BLL;
namespace MyApp.DAL
@danielgreen
danielgreen / BlockPageOnSubmit.js
Last active December 18, 2015 17:59
An unobtrusive way to block the page on form submission. Requires jQuery Validation to be active on the form (either directly or via unobtrusive validation), and the submithandlerfix to be applied (https://gist.github.com/danielgreen/5669244). If jQuery Validation is not active on the form then we need another solution e.g. just bind to onsubmit
$("form[data-block-page-on-submit]").each(function () {
$(this).validate().settings.submitHandler = function () {
window.BlockPage();
return true;
};
});