Archive

Archive for December, 2011

ASP.NET MVC String Editor Template which handles the StringLength attribute

December 20, 2011 Leave a comment

Interestingly enough I was searching for a way for MVC to automatically handle the stringlength property. Seemed quite straightforward to implement, but somehow there is no available code out there to figure this out. Using the Html.EditorFor just creates a no frills textbox, which is rather irritating.

Logic walkthrough

Basically what the template does is to attempt to find the StringLength attribute from the ModelMetadata, and from there get the validation parameters.

Once it finds the appropriate parameter (in this case max), it will assign this to a string dictionary.

Finally all is wrapped up with a call to Html.Textbox

Usage

to use this ‘custom’ editor template, what you need to do will be

@Html.EditorFor(model => model.strcolumn)

or if you want to specify the width as well

@Html.EditorFor(model => model.strcolumn, new { width = “300px” })

NOTE: This works if you specify Text as the datatype, otherwise you will need to change the top to include the template name

Code Segment (Text.cshtml)

Note that the following code segment is in razor format, but you can easily change that to a webcontrol.

@* Specifies that this is for the string data type *@
@model String
@{
//Create a dictionary to hold all the html attributes to be applied, this follows the requirement for the Html.Textbox last property which requires IDictionary<string, object>
var o = new System.Collections.Generic.Dictionary<string, object>();

//add in the default css class, notice that the @ is not required in this case
o.Add(“class”, “text-box single-line”);

//Attempt to find the validation attribute for maxlength
foreach (var r in ViewData.ModelMetadata.GetValidators(this.ViewContext.Controller.ControllerContext))
{
//What we are looking for is a validator of type StringLengthAttributeAdapter
if (r is StringLengthAttributeAdapter)
{
var attr = (StringLengthAttributeAdapter)r;

var ValidationParameters = attr.GetClientValidationRules().First().ValidationParameters;

//Get the validation parameters, maxlength is a required parameter so it is definately there, but just check in case for some reason it is not there
if (ValidationParameters.ContainsKey(“max”))
{
//add the maxlength property to the dictionary
o.Add(“maxlength”, (int)ValidationParameters[“max”]);
}
}
}

//additional check to see if there is a width property specified
if (ViewData[“width”] != null)
{
o.Add(“style”, “width: ” + ViewData[“width”].ToString());
}

//Finally emit the html for the textbox
@Html.TextBox(“”, ViewData.TemplateInfo.FormattedModelValue, o);
}

Advertisements
Categories: AJAX, ASP.NET, MVC

Cloud Shared Notepad–Beta

December 16, 2011 Leave a comment

This is the pre beta version of the cloud shared notepad, its free for all to use (unless it becomes so popular that my azure account starts charging me)

Anyway I acknowledge that there are definitely issues with the GUI, so please bear with me for a while.

Just some points to note

  1. Below the cloudsharenotepad.cloudapp.net/notes is actually a textbox, you have to enter your note url there (in this example it is myurlhere)
  2. You can enter your notify email, I have yet to implement this function, so for now it is purely cosmetic

image

 

Thanks for your support! Smile

Categories: ASP.NET, Azure

Publishing to Windows Azure from Visual Studio 2010

December 16, 2011 Leave a comment

 

Here are the steps to publish to Windows Azure from inside Visual Studio 2010

  1. From inside Visual Studio, right click the Azure project (the icon looks like a blue globe)
  2. image

  3. Click on “Publish”
  4. You will see the screen below
    image
  5. If you have yet to import your publish settings, click on the hyperlink to generate the publishing file
  6. Once you get the file, use the import button to import your settings file
  7. You should now have a subscription available for selection
    image
  8. If you do not have any hosted services, you will see the screen below. Else skip to step 9
    image
  9. Fill in your hosted service name and preferred data centre location and click “OK”.
  10. In the publish settings, make the necessary changes and click “Publish”
  11. Windows Azure Activity Log will appear and you can see the deployment status there. Do take note that this may take quite a while to deploy even for a simple app.
    image
  12. Once it is complete, the status will reflect to show it is completed and you will be able to see the url to your app
    image
    Enjoy!
Categories: ASP.NET, Azure

Azure TableServiceContext presents an error with CreateQuery function

December 16, 2011 2 comments

If you were using azure table storage and found the following error when writing the CreateQuery function

image

does not contain a definition for’ ‘CreateQuery’ and no extension method ‘CreateQuery’ accepting a first argument of type..

 

What is happening is that other than adding Microsoft.WindowsAzure.StorageClient, you aldo need to add System.Data.Services.Client, which Visual Studio actually tells you when you attempt to build the project.

 

Error    1    The type ‘System.Data.Services.Client.DataServiceContext’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Data.Services.Client, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’.   

 

Once you added the reference, the error will automatically go away.

 

Now if only there was an easy way to automatically add the missing reference in since Visual Studio knows the exact error and also what should be done..

Categories: ASP.NET, Azure

Windows SQL Azure Firewall

December 15, 2011 2 comments

 

For those who are new to SQL Azure, there are firewall settings that you need to take note of when you first create a SQL Database.

 

By default the firewall is set to block ALL external access

 

So if you wish to enable ALL access (aka disable the firewall), you can use the following settings

image

Categories: Azure, SQL Server

Dealextreme Affliate

December 9, 2011 Leave a comment

if you are shopping at dealextreme, click this link and help me get some commission, thanks.

http://www.dealextreme.com/default.dx/r.43428218

Categories: Uncategorized