Friday, 6 December 2013

Working with twitter bootstrap in Asp.Net MVC4

At time when i was struggling in my project for UI development my manager told me about this great thing. At start i found it little difficult to work with but later when i got to know about its huge benefits and swiftness i became a fan of it. I was like "Oh, we can make complete project out from it.".

What really twitter bootstrap is who made it and why, all these questions i will answer here only.
2 Designers from twitter (Mark Otto and Jacob Thornton) actually made it now its an open source project hosted on github.

For Asp.net MVC projects you directly have to install the 2 NuGet packages. They are:
  1. twitter.bootstrap.mvc4
  2. twitter.bootstrap.mvc4.htmlhelpers
Actually you have one more option of installing the samples that those guys have made. For that you have to install a separate package:  twitter.bootstrap.samples.mvc4

Once through with all this you can continue with creating Models, Controllers, Generating Views or Creating Views in your project. All the Views will use Bootstrap Layout for Textboxes, Checkboxes. But for buttons you have to apply the class seprately.
Following are some examples of controls with bootstrap styling.
For getting more out of the Bootstrap you seriously need to know about all the style elements which you can get a good understanding from the following link:

I hope that this article will help you in using Twitter bootstrap for MVC4.
For any queries write comment.

Distinct Keyword in LINQ

Few days back when I was trying to Filter my result set using Linq Distinct keyword I got into a situation. The keyword didn't worked for me, I was getting same result set with or without Linq.

I Goggled for the solution, But most of the solutions offered to user the Group By operator. which is as follows:

Take an entity that contains following fields:
id, Name.
public class MyDemo
{

public int id{get; set;};

public string Name {get; set;};

}
MyDemo ListData = new MyDemo
{
id=1, Name="Alpha";
id=2, Name="Beta";
id=1, Name="Alpha";
id=3, Name="Gamma";
}
MyDemo ListResult;


ListResult = (ListData.Select(t => t).GroupBy(x => x.Name).Select(y => y.First()).ToList()

Distinct Keyword is little tricky in Linq. At first it would not work same like Distinct in sql server Linq to Object behaves little weird because all are distinct object references so the other way round to this problem is use a GroupBy Operator.

We can get the solution to this problem by introducing an anonymous type to store the result and in that case we will get the desired result.

var result =  (from d in ListData select new { d.id, d.Name }).Distinct();