Skip to main content


Showing posts from 2009

To View Model or not to View Model?

Up to now in developing web applications using ASP.Net MVC I've eschewed the use of View Models and have simply passed my Domain Model objects into views, along with supplementary information as needed in the ViewData Dictionary . This technique allows the domain model object to be be made available in a strongly typed manner within the view, and to use model binding to put the object back together following form posts for create and update methods. Certainly for the scale of application I've built so far this works just fine. In cases where I want to use additional information passed in the loosely typed ViewData dictionary, I'm able to cast to a typed variable at the top of the view in order to use it in a strongly typed manner from then on. And if I've needed to pass additional information back from my form posts, these can be added as additional parameters to the controller action set up to receive the post. The following code illustrates this. The first meth

Count Active Sessions in ASP.Net

In order to monitor load on a site I've built and support, I wanted to add a simple counter to show how many active sessions were currently being handled by the web application. The first technique was to use a counter variable held in the Application context. This was pretty straightforward - in global.asax increment the counter in the Session_Start event and decrement in the Session_End:   1    Sub Session_Start( ByVal sender As Object , ByVal e As EventArgs)   2     3        'Increment session count   4        Application.Lock()   5        Application( "SessionCount" ) += 1   6        Application.UnLock()   7     8    End Sub   9      10    Sub Session_End( ByVal sender As Object , ByVal e As EventArgs)    11      12        'Decrement session count    13        Application.Lock()    14        Application( "SessionCount" ) = Application( "SessionCount" ) - 1    15        Application.UnLock()    16      17    End Sub Unfortuna

Paging with SQL Server and MySQL

Paging sets of database records in a web application efficiently has long been a relatively tricky undertaking. Whilst web form controls such as the GridView offer control based paging very easily, they will still hit the database for all the records before processing the paging within the web application, and hence unnecessary amounts of data are passed between database and web server. Database based techniques - only retrieving the particular rows you require in each request - are therefore generally to be preferred. But some of those provide their own headaches - for example as well as retrieving the particular set of rows for a page, you will often want to know what the full count of records to be able to display a message along the lines of: 200 records found. Page 1 of 20 . SQL Server Paging Prior to the 2005 version, I would do this using SQL Server with two queries - one to get the page of data and another, using the same WHERE clause, to get the count. With SQL Serve

Integrating Paypal Payment Pro (Direct Payment)

I've recently had need to integrate PayPal payments on a website, using their Payments Pro interface. It's actually very straightforward, although initally didn't appear so as the information they provide on their website and forums is rather confusing. This is primarily due to the different versions of their API that have been provided historically, plus there are a number of different techniques to access it. All I need was a simple request to authorise a set of card details, getting back a response to allow the order to either be processed appropriately. Pulling together with my own code a few different examples online, the Paypal documentation and some advice from their technical support led me to this function - which I'll post in the hope of helping rather than hindering someone else in the same position!     1   private void makePayPalPayment()     2  {     3    //Open log file     4    TextWriter objLogFile;     5    objLogFile = File.AppendText(Configurati

MVC/Jquery Calendar

One of the main pluses of working with the ASP.Net MVC framework is of course the separation of concerns it promotes with the split between the controller and the view in the UI. This is not only apparent in the standard method of having the controller obtain the data required for display, and passing this to a view for display as XHTML. The same clear separation of purpose can be made use of when the output of the view is other formats too. In a recent project I've had need to represent a calendar of events - a simple side bar to the website that displays a calendar view with days that have events associated with them highlighted. Clicking on the day displays the list of events below. In implementing this feature I made use of the controller and view pattern in three distinct ways. Having a controller render an XHTML page view Having a controller render JSON data for use from an AJAX request . Having a controller render a partial view to support an AJAX page update . Applicat