Saturday, December 20, 2014

jQuery UI Autocomplete Changes in 1.10

Legacy 1.9 Code:
$("#searchBox").data("uiAutocomplete")._renderItem = function (ul, item) {
 return renderAutoCompleteItems(ul, item);
};

renderAutoCompleteItems = function(ul, item) {
 return $("<li />")
   .data("item.autocomplete", item)
   .append("<a><img style='width:50px; height:50px;' src='" + item.icon + "' /> " + item.label + "</a>")
   .appendTo(ul);
};

Updated 1.10 Code:
$("#searchBox").data("ui-autocomplete")._renderItem = function (ul, item) {
 return renderAutoCompleteItems(ul, item);
};

renderAutoCompleteItems = function(ul, item) {
 return $("<li />")
   .data("ui-autocomplete-item", item)
   .append("<a><img style='width:50px; height:50px;' src='" + item.icon + "' /> " + item.label + "</a>")
   .appendTo(ul);
};

Friday, December 12, 2014

Upgrade from ASP.NET Identity 1.0 to 2.0

View -> Package Manager Console
PM > Enable-Migrations
PM > Add-Migration UpdateIdentifyName
PM > Update-Database -verbose

using System.Data.Entity.Migrations;
    
    public partial class UpdateIdentifyName : DbMigration
    {
        public override void Up()
        {
            RenameColumn(table: "dbo.AspNetUserClaims", name: "User_Id", newName: "UserId");
            RenameIndex(table: "dbo.AspNetUserClaims", name: "IX_User_Id", newName: "IX_UserId");
            DropPrimaryKey("dbo.AspNetUserLogins");
            AddColumn("dbo.AspNetUsers", "FirstName", c => c.String(nullable: false, maxLength: 256));
            AddColumn("dbo.AspNetUsers", "LastName", c => c.String(nullable: false, maxLength: 256));
            AddColumn("dbo.AspNetUsers", "Email", c => c.String(maxLength: 256));
            AddColumn("dbo.AspNetUsers", "EmailConfirmed", c => c.Boolean(nullable: false));
            AddColumn("dbo.AspNetUsers", "PhoneNumber", c => c.String());
            AddColumn("dbo.AspNetUsers", "PhoneNumberConfirmed", c => c.Boolean(nullable: false));
            AddColumn("dbo.AspNetUsers", "TwoFactorEnabled", c => c.Boolean(nullable: false));
            AddColumn("dbo.AspNetUsers", "LockoutEndDateUtc", c => c.DateTime());
            AddColumn("dbo.AspNetUsers", "LockoutEnabled", c => c.Boolean(nullable: false));
            AddColumn("dbo.AspNetUsers", "AccessFailedCount", c => c.Int(nullable: false));
            AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: false, maxLength: 256));
            AddColumn("dbo.AspNetUsers", "CreatedDateTime", c => c.DateTime(nullable: false));
            AlterColumn("dbo.AspNetRoles", "Name", c => c.String(nullable: false, maxLength: 256));
            AddPrimaryKey("dbo.AspNetUserLogins", new[] { "LoginProvider", "ProviderKey", "UserId" });
            CreateIndex("dbo.AspNetUsers", "UserName", unique: true, name: "UserNameIndex");
            CreateIndex("dbo.AspNetRoles", "Name", unique: true, name: "RoleNameIndex");
            DropColumn("dbo.AspNetUsers", "Discriminator");
        } 
    }

Tuesday, December 9, 2014

ASP.NET MVC File Upload through jQuery AJAX

HTML:
<input type="file" name="file" id="file" />

Javascript Submit:
var formData = new FormData();
var file = $("#file")[0].files[0];
formData.append("file", file);
formData.append("SourceId", 1234);
ajaxRequestFormData(window.urls.uploadAttachment, formData)

jQuery AJAX:
ajaxRequestFormData = function (url, model) {
$.ajax({
url: url,
type: "POST",
cache: false,
data: model,
dataType: "json",
contentType: false,
processData: false,
beforeSend: showProgress()
}).done(function (data) {
...
hideProgress();
}).fail(function () {
...
});
};

Controller Action:
public virtual ActionResult UploadAttachment(AttachmentDto attachment)
{
var file = Request.Files[0];
attachment.File = file;

var sourceId = attachment.SourceId;

...
}

Domain Class:
public class AttachmentDto
{
public HttpPostedFileBase File { get; set; }
public decimal SourceId { get; set; }
...
}