Thursday, 26 July 2012

TextBoxFor and DisplayFormatAttribute


Usage

Model

[DisplayFormat(DataFormatString = "{0:0.##}", ApplyFormatInEditMode = true, NullDisplayText = "")]
public decimal? Quantity { get; set; }

View

@Html.FormattedTextBoxFor(m => m.Quantity)
 

Source

public static MvcHtmlString FormattedTextBoxFor(this HtmlHelper html, Expression<Func> expression, object htmlAttributes = null)
{
    var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
    var formatString = metadata.EditFormatString;
    if (formatString == null) return html.TextBoxFor(expression, htmlAttributes);
    var value = String.Format(formatString, metadata.Model);
    var name = ExpressionHelper.GetExpressionText(expression);
    return html.TextBox(name, value, htmlAttributes);
}

Monday, 23 February 2009

Html Encoding in MonoRail Views

I was surprised that there didn't seem to be a method for HTML encoding using the HtmlHelper in MonoRail. At least, that's what the assembly manifest and documentation would lead you to believe.

It turns out that there is an HtmlEncode method on the AbstractHelper base class, so you can just call HtmlHelper.HtmlEncode() after all!

Monday, 14 April 2008

Setting up Netgear wireless adapters in Ubuntu

I recently decided I should really start using Linux properly, at least at home. I went for Ubuntu, which from what I can tell is one of the most popular distributions. The version I'm using is 7.10 (Gutsy Gibbon).

Unfortunately I found that my Netgear WG111 USB wireless adapter was not currently supported on Linux, and I had to jump through a few hoops to get online. I've decided to document the process I went through to set this up. I learned a lot about Linux in a very short space of time by doing this and I'll detail what I learned here. Hopefully others will be able to make use of this.

N.b. The Ubuntu forums were a great deal of help, most of the information here was learned from there, in particular the post "Installing a Netgear WG111v3 or other similar wireless" by stooshbunutu.

The adapter I have is a WG111v3, although I believe this should work for other versions as well as other Netgear wireless adapters such as the WG311 PCI card and WG511 PC card/PCMCIA adapter by downloading the relevant .inf files (see below).

N.b. It's definitely worth checking that your Ubuntu Live CD is free of errors if you haven't already. Mine turned out to be corrupted, despite the fact that I was initially able to install Ubuntu to hard disk without any reported problems. This caused a lot of unnecessary frustration. There is an option to check the CD in the CD's boot menu.

This is the method I used to get everything working.

Step One - Install NDISwrapper

The first thing you'll need is a tool called NDISwrapper. If you have a Windows driver for your network adapter (most do I assume), NDISwrapper allows you to use this Windows driver under Linux.

NDISwrapper is actually included with Ubuntu 7.10, but it's not installed by default. You can install it from the Synaptic Package Manager (System > Administration > Synaptic Package Manager) or by opening a terminal window and using the apt-get command. I failed to get either of these to work due to the CD corruption mentioned above, so I ended up downloading and building the latest version. Having the latest version may be advantageous anyway, and I learned a lot through doing my first Linux build, so this is the method I will detail here.

Download the NDISwrapper source code

You should be able to download the latest version by finding NDISwrapper on SourceForge. At the time of writing this was version 1.52. If like me you don't have a wired connection you'll be having to do this on another machine. If so, copy the file to your home directory (Places > Home Folder) in Ubuntu.

Open a Terminal Window

Open a terminal window (Applications > Accessories > Terminal). From here on you'll be issuing some commands that need super user privileges. To obtain these, use the following command:

sudo su

You'll probably be asked to enter your password.

N.b. If you close your terminal window and re-open it you'll probably need to issue this command again.

Install the build-essential Package

When I tried to build NDISwrapper I got a whole raft of errors and warnings. I was about ready to give up at this point, as a Linux beginner I wasn't about to try and debug someone else's source code! Curiosity got the better of me and I scrolled up to find out where the first error had occured. "Couldn't find stdlib.h". I've been a developer long enough to know that this is pretty fundamental! A bit of research and I found out you need to install the "build-essential" package to get the standard libraries. You can do this through Synaptic (System > Administration > Package Manager) or by issuing the apt-get command as follows:

apt-get install build-essential

Remove existing NDISwrapper

I'm not sure if this step is necessary, but I ran it as instructed in the forum post mentioned above. I guess the following command should uninstall any existing version:

apt-get remove ndiswrapper-common

Extract the files onto your hard disk

The file you've downloaded should be a gzipped tar file (extension tar.gz). tar combines mutiple files and directories into one file, and gzip compresses this file to make it smaller. The same operations as performed for Windows with zip files. You can use the tar command-line utility to extract the source code:

Go to your terminal window. You should be in your home directory so you can extract the source code by typing in the following command:

tar xvvf ndiswrapper-1.52.tar.gz

N.b. You may be using a newer version, in which case the file name will be different. You can use tab completion to complete the file name for you, as you start typing "ndis" try pressing the [tab] key to fill in the rest of the name.

This should have extracted the files into the ndiswrapper-version directory. Navigate into this directory using the cd command:

cd ndiswrapper-1.52

N.b. Again the version number may differ, and [tab] can help you here.

Build NDISwrapper

Hopefully now you can build NDISwrapper by issuing the make command.

make install

Step Two - Find and Set Up the Windows Driver

The next thing you'll need to do is find the Windows driver for your adapter and set it up with NDISwrapper.

Download the Windows Driver

I found the Windows driver for my WG111v3 here:

http://www.avengergear.com/upload/WG111v3.tar.bz2

Windows drivers for other Netgear adapters should be easy to find through the Netgear Support website.

As with NDISwrapper, copy the downloaded file into your home directory and use tar to extract the files:

tar xvvf WG111v3.tar.bz2

Note that Linux is case sensitive, so in the example above you'll need to make sure the 'W' and 'G' are capitalised.

Configure NDISwrapper to use the Windows Driver

I'm now assuming the directories to which NDISwrapper and the Netgear drivers have been extracted are sat at the same level (under your home directory).

From the NDISwrapper directory, you would issue the following command in order to load the Netgear drivers (adjusting the path for different drivers):

ndiswrapper -i ../WG111/WG111v3.inf

Get Up and Running

The next bit is a bit of a mystery to me, I intend to find out what's happening and will update this post when I do! Anyway it involves issuing these commands:

depmod -a
modprobe ndiswrapper
ndiswrapper -m


Hopefully now everything should be working and you'll be able to set up your wireless network.

Step Three - Handle Rebooting

After this great success, I was dismayed when next time I booted my machine I was back to square one. I wandered off to the gym and returned determined to work out what was going on. It turns out you need to do a little more work in order to have your wireless adapter set up each time you boot into Ubuntu.

Add NDISwrapper to boot

Again I'm not sure how this bit works, but intend to elaborate as I find out:

ndiswrapper -m

Then add the line "ndiswrapper" to the the /etc/modules file which you can edit using the following command:

gedit /etc/modules

Hopefully that's it!

Feeding Time for BigJump

We decided that the BigJump website was hungry for new, dynamic content. What better way to feed it than with our own blogs? We are going to publish this (my) blog and those of other BigJump staff, by importing the RSS feeds generated by Google Blogger, their original home.

Implementing this has given us the chance to utilise .net 3.5's Linq to XML facility, which we've used to parse the RSS. It turns out that Scott Guthrie has posted a guide on doing exactly this.

Wednesday, 9 April 2008

MonoRail Form Validation Documentation

This post describes how to use MonoRail's (v1.0 RC3) built in form validation.

The javascript validation used is Dexagogo's Really Easy Field Validation.

The error messages usually use the title attribute of the input field, which can be inserted automatically by MonoRail. In order to set a custom error message, add a div with id of the form: advice-element-id, where element-id is the id of the input field being validated.

Packaging MonoRail View Components in an Assembly

I recently found myself in a situation where I wanted to package some view components in a separate assembly for reuse, in much the same way as you'd create an assembly of server controls in web forms/ASP.NET.

Obviously this means you need to get rid of any views or compile them into the assembly somehow. Taking a look into the Castle.MonoRail.ViewComponents source code, it turns out there is a "RenderText" method that you can use within your ViewComponent to output view text directly.