ASP.NET MVC 3 Data annotations – Compare Error

I had a problem when  I was doing a registration form using MVC3, where the user has to confirm the password and then using Data annotations attribute [Compare] to make sure that the password and confirm password was equal. But there is a problem if this attribute I think. When typed different password the page wasn’t validated. The solution was change a little bit the code in the jquery.validate.unobtrusive.js file:


element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];

to

element = $(options.form).find(":input[name=" + fullOtherName.replace(".", "\\.") + "]")[0];

When you use the code below, everything I’ll be fine:


[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

 

Solution source: http://connect.microsoft.com/VisualStudio/feedback/details/665793/jquery-unobtrusive-validate-equalto-fails-with-compare-attribute

 

 

Entity Framework Video on the Channel 9

Do you know Channel 9? Awesome place to learn about new technologies, there are great videos and one of this is about Entity Framework. Rowan Miller and Diego Vega of the Entity Framework team show us in two videos parts about the concepts of Code First, Database First and Model First. If you are looking for to know more about Entity Framework take a look on this videos:

Visual Studio Toolbox Entity Framework Part 1

Visual Studio Toolbox Entity Framework Part 2

Developer Center – ADO.NET Entity Framework

 

 

 

HOW TO: Sort a GridView using List as a datasource

Hey, A few days ago I had post this : HOW TO: Using sorting on GridView without a DataSourceControl, but it was explaining how make this using DataTable as a datasource. I was doing something in a project, when suddenly 🙂 I need to sort a gridview columns, but my datasource was a ListColletion, like this: List<Costumer>, I made a research found out different ways to make the sort works. But the simplest way is this one:


private string ConvertSortDirectionToSql(SortDirection sortDirection)
{

string newSortDirection = string.Empty;

switch (sortDirection) {
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}

protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{

string previousSortExpression=(string)ViewState["previousGVSortKey"];
string sortExpression=e.SortExpression;
SortDirection sortDirection = e.SortDirection;

if (sortExpression.Equals(previousSortExpression))
{
sortDirection = SortDirection.Descending;
ViewState["previousGVSortKey"] = string.Empty;
}
else {
ViewState["previousGVSortKey"] = sortExpression;
}

     List<Costumer> list = (List<Costumer>)GridView.DataSource;
     if (list.Count > 0) {
        var query = from c in list
                    select c;

           if (sortDirection == SortDirection.Ascending)
{
if (sortExpression == "FIRST_NAME")
query=query.OrderBy(c => c.FirstName);

if (sortExpression == "LAST_NAME")
query=query.OrderBy(c => c.LastName);

if (sortExpression == "EMAIL")
query = query.OrderBy(c => c.Email);

if (sortExpression == "ADDRESS")
query = query.OrderBy(c => c.Address);
}
else {
if (sortExpression == "FIRST_NAME")
query = query.OrderByDescending(c => c.FirstName);

if (sortExpression == "LAST_NAME")
query = query.OrderByDescending(c => c.LastName);

if (sortExpression == "EMAIL")
query = query.OrderByDescending(c => c.Email);

if (sortExpression == "ADDRESS")
query = query.OrderByDescending(c => c.Address);
}

GridView.DataSource = query.ToList();
GridView.DataBind();
     }
}

I know there are a lot of better way to do that, but for now it is working, maybe I’ll try to make something better.

Using Forms Authentication. SetAuthCookie

I know, it’s old :-), but just to keep this information available for someone, when you are using the  FormsAuthentication.Authenticate(login, password) the authentication cookie/ticket is created automatically to make sure that the user is authenticated, but sometimes you have your own login form, your own authenticate method, so you need to call the method FormsAuthentication.SetAuthCookie(value, bool) to create the cookie/ticket for the authenticate user.

If you are using the Authentication mode to Forms and when you call this method (HttpContext.Current.User.Identity.IsAuthenticated) to verify if the user is Authenticated, will be true only if exists an authentication ticket cookie (I belive can be through Cookie or SqlAuthCookie).

You can find more explanation about this in the follows links:

Explained: Forms Authentication in ASP.NET 2.0

http://msdn.microsoft.com/en-us/library/ff647070.aspx

How To: Use Forms Authentication with SQL Server in ASP.NET 2.0

http://msdn.microsoft.com/en-us/library/ms998317.aspx

Nested classes to structuring Unit tests approach

I had read a great post in the Phil Haack blog, you can access here (Structuring Unit Tests) about how you can organize your unit tests, making a nested class for each method that you going to write, it is strange when you hear this, but after you understand this principle, I think you going to agree with this approach. It is a better idea to organize your unit test class, to find faster you test methods. Who write unit test know that sometimes you feel a little lost between a lot of methods and classes.

Take a look:

Structuring Unit Tests

Dependency Injection in .NET

I’m new in this subject, I just started to hear about four months ago, so I had read this book (Dependency Injection in .NET by Mark Seemann) what is awesome, good explanation and examples.  Where you can find a lot of information to learn about Dependency Injection.

The most popular article about Dependency Injection is without doubt the article (Inversion of Control Containers and the Dependency Injection pattern by Martin Fowler) where you can understand the concept, the pro and cons, forms of Dependency Injection and more.

The book (Patterns of Enterprise Application Architecture by Martin Fowler) have a lot information about Dependency Injection and other Patterns)

Article:

http://martinfowler.com/articles/injection.html

Books:

Dependency Injection in .NET by Mark Seemann

Patterns of Enterprise Application Architecture by Martin Fowler

Containers:

http://ninject.org/

http://structuremap.net/structuremap/