Monday, May 17, 2010

ASP.NET MVC 2 Client side validation using jQuery validation plug-in

ASP.NET MVC 2 has validation architecture that support both server-side and client-side validation. ScottGu describes in details how to get validation works in his post ASP.NET MVC 2: Model Validation.

The best feature in client-side architecture is that you may use alternative java scripts validation libraries like jQuery validation.

There are a steps to get client-side validation works using jQuery validation plug-in:

  1. Download and add to your ASP.NET MVC project following java scripts:
    I assume you put all scripts into /Scripts folder of your MVC application
  2. Enable client side validation
    • add three JavaScript references to your view

    • write one line of code in the view before form
That's it! Now you have Client-side validation using jQuery validation plug-in!

Thursday, April 29, 2010

S#arp Arcitecture 1.5 - running T4 teplate error

I decided to build some project using S#arp Architecture. I am using VS 2010.

I went through all installation steps and created my first project S#arp Architecture Application.

My next step was running scaffolding generator. And then I got the troubles.

Any time I run custom tool, I get two error messages:

Compiling transformation: Metadata file 'Inflector.Net.dll' could not be found
Compiling transformation: Metadata file 'SharpArch.Core.dll' could not be found

I checked the references - they was set correctly. I was stuck but I didn't want to surrender, and after hours of googling and voodoo I got the solution.

The problem is solved by specifying the absolute path to the referenced assembly in template file (@assembly directive)

I found two files where Inflector.Net.dll and SharpArch.Core.dll are referenced:

[solution folder]\tools\CrudScaffolding\EntityScaffoldingDetails.tt
[solution folder]\tools\CrudScaffolding\Templates\BaseTemplate.tt

and replaced following rows

<#@ assembly name="SharpArch.Core.dll" #>

with

<#@ assembly name="[solution folder]\lib\SharpArch.Core.dll" #>

and

<#@ assembly name="Inflector.Net.dll" #>

with

<#@ assembly name="[solution folder]\lib\Inflector.Net.dll" #>


No error anymore!

Saturday, March 13, 2010

IZWebFileManager 2.5.3 has been released

IZWebFileManager 2.5.3 has been released

It includes:
* Thumbnail view fix (http://www.izwebfilemanager.com/forums/thread/445.aspx)
* New Netherlands/Dutch translation (by Marcel van den Arend)

The release is available for download from Google Code
https://code.google.com/p/izwebfilemanager/

Cheers!

Wednesday, February 10, 2010

Query the database with NHibernate 3.0

Introduction

NHibernate gives you many alternative APIs for querying database. There is HQL, which is similar to SQL, Linq to NHibernate and finally ICriteria.
And once you start to use NHibernate in your project, you have to decide which API to use. Since NH 3.0 you have one more option. It is QueryOver API.

In this post I want to compare a usage of different APIs for a simple query.

For this sample I created a console application with embedded database. Full source code is available at google code.
In my sample I have a database with one table Emploee (Id, Name, Age, Salary) and I want to calculate average salary of employees who's between 25 to 30 years old.

Lets start!

HQL

Since I was not familiar with NH (but had an experience with SQL), I found HQL is easiest way to get started with.
There is query:


var salary = session.CreateQuery(
@"
select
avg(employee.Salary)
from
Employee employee
where
employee.Age >= 25 and employee.Age <=30
"

).UniqueResult<double>();


Looks good, readable but not strongly typed.

Go next one.

ICriteria

The ICriteria API is NHibernate's implementation of Query Object.


var salary = session.CreateCriteria(typeof(Employee))
.Add(Restrictions.Between("Age", 25, 30))
.SetProjection(Projections.Avg("Salary"))
.UniqueResult<double>();


Much better, but still has some "magic strings". I am looking for strongly typed code where refactoring tools like 'Find All References', and 'Refactor->Rename' will work perfectly.

Linq

NHibernate 3.0 has a built-in Linq provider.


var query = from employee in session.Query<Employee>()
where employee.Age >= 25 && employee.Age <= 30
select employee.Salary;

var salary = query.Average();


Pretty good! Strongly typed, readable. But it uses the fact that Enumerable has Average in this case. Would we need a more complicated projection, it would be not available in LINQ. Also, LINQ syntax is still confusing for some people. It just looks different than surrounding C# code.

Keeping looking for a perfect.

QueryOver

QueryOver combines the use of Extension Methods and Lambda Expressions to provide a statically typesafe wrapper round the ICriteria API.


var salary = session.QueryOver<Employee>()
.WhereRestrictionOn(e => e.Age).IsBetween(25).And(30)
.Select(e1 => e1.SelectAvg(e2 => e2.Salary))
.List<double>()
.Single();


Perfect!

In my opinion this one is the best.




Tuesday, January 5, 2010

IZWebFileManager 2.5.2 has been released

IZWebFileManager 2.5.2 has been released

It includes:
* FileDownload event
* New Swedish translation (by Peter Strömblad)
* IE8 compatibility fixes

The release is available for download from Google Code
https://code.google.com/p/izwebfilemanager/

Cheers!

Monday, November 30, 2009

Mapping URLs using ASP.NET 2.0 Web Forms

ASP.NET 4.0 is coming with new feature - URL routing with ASP.NET Web Forms

It is pretty cool feature, but require upgrade up to .NET 4.0.
I am not sure that such a good feature is a real reason to get upgraded, and in most cases you will stay with current version on .NET.

But I want it and I want it now, with .NET 3.5. I am going to implement it!

Tuesday, November 24, 2009

ASP.NET MVC Model Binding and Model's fields

A Model Binding in MVC provides a simple way to map posted form values to a .NET Framework type and pass the type to an action method as a parameter.

Everything works fine while a model has public properties like this:

public Person
{
public string Id {get;set;}
public string Name {get;set;}
...
}

But if you try to use public fields instead of properties like this:

public Person
{
public string Id;
public string Name;
...
}

you will be surprised to get the model passed to controller's action with no assigned fields.

It happens because ASP.NET MVC use DefaultModelBinder by default which deals with properties but not with fields when map posted form values.

Of course you can use properties instead of fields wherever you need model bindings. But it is an annoying limitation.

ASP.NET MVC allows us to override both the default model binder, as well as add custom model binders, through the ModelBinders static class:


ModelBinders.Binders.DefaultBinder = new SomeCustomDefaultBinder();
ModelBinders.Binders.Add(typeof(Person), new PersonModelBinder());

There are more blog entries about ASP.NET MVC Model Binding
6 Tips for ASP.NET MVC Model Binding
A better Model Binder