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 / 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;
};
});
@danielgreen
danielgreen / Readme.md
Created June 13, 2013 11:08
MVC tips - don't return a View from an HTTP Post action

A Post action should redirect to a Get action rather than returning a View. This avoids the annoyance of the browser displaying the "do you want to resubmit the form" message if the user tries to refresh the page after the Post.

@danielgreen
danielgreen / Readme.md
Created June 13, 2013 11:06
Check for unsaved changes on a page

Develop an unobtrusive approach - if a form has the data-warn-unsaved="true" attribute then attach change event handlers to its fields. Do this for all such forms on the page. The handlers can set data-unsaved-changes="true" on the form.

Set window.onbeforeunload to a function that checks for any forms with data-unsaved-changes="true". If any such forms are found, return some text to be displayed as a prompt to the user.

When a form's contents are saved successfully, we want to set data-unsaved-changes="false". Define an event that can be invoked upon a successful save, in order to set the attribute's value?

Also see http://codethug.com/2013/02/01/knockout-binding-for-onbeforeunload/

@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 / DateTimeExtensions.cs
Created June 13, 2013 10:33
DateTimeExtensions - useful extension methods for date processing
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Utility
{
// http://dotnetslackers.com/articles/aspnet/5-Helpful-DateTime-Extension-Methods.aspx
@danielgreen
danielgreen / OrphanedUsers.sql
Created June 12, 2013 16:11
SQL script to identify orphaned database users (where the SQL Server login or Windows login is missing)
DECLARE @cmd1 VARCHAR(MAX)
DECLARE @cmd2 VARCHAR(MAX)
IF EXISTS (SELECT object_id FROM tempdb.sys.objects WHERE name like '#OrphanUsers%')
DROP TABLE #OrphanUsers
CREATE TABLE #OrphanUsers
(
UserName VARCHAR(50) NULL,
userSID VARBINARY(85) NULL,
dbName VARCHAR(50) NULL
@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 / 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 / NuGet.txt
Last active December 17, 2015 21:49
Useful NuGet packages
WorkspaceReloader
AutoMapper
DataAnnotationsExtensions.MVC3
ELMAH.MVC
Modernizr
Twitter.Bootstrap
SlickGrid
Select2.js
jQuery
jQuery.UI.Combined
@danielgreen
danielgreen / Script.PostDeployment.sql
Created May 30, 2013 12:23
Visual Studio 2012 Database projects (which are based on SQL Server Data Tools aka SSDT) allow the developer to define a database schema with tables, keys, indexes, stored procedures, etc. Out of the box, a Database project in VS 2012 does not provide an easy way to define upgrade scripts or environment-specific security (logins, users, roles, e…
/* This script should have a Build Action of PostDeploy within the Visual Studio project. */
/*
Post-Deployment Script Template
--------------------------------------------------------------------------------------
This file contains SQL statements that will be appended to the build script.
Use SQLCMD syntax to include a file in the post-deployment script.
Example: :r .\myfile.sql
Use SQLCMD syntax to reference a variable in the post-deployment script.
Example: :setvar TableName MyTable