Monday 28 October 2013

Click Here to Download SetDefaultRouteInMVC.zip Example.

You can set Default route value for URL in MVC. Traditional our route pattern is like '{controller}/{action}/{id}'. This is going to fail if we pass URL like '{controller}/{action}'.

There are many scenarios in our day to day software application we require both pattern. For that you can set 'Id' as default parameter so that if we did not pass 'id' in query string it will not raise errors and execute our desired 'action'.

We can configure Route in 'App_Start/RouteConfig.cs' file using this 'routes.MapRoute' method, where 'routes' is the object of 'RouteCollection' class. In 'MapRoute' method we can set Default values. Code is something like below :
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { id = UrlParameter.Optional }
    );

Here is example for this. 

Wednesday 16 October 2013

You can query objects using SelectMany LINQ method and get result according to your given criteria. You can also query more than one criteria.

Here is example for this.

In this example we take "books.xml". Now we query on element "author" whose sub element is 'firstname' and 'lastname' and we only want list of authors whose name is either 'Pratik' or 'David'.

Tuesday 8 October 2013

We can do Range validation using annotations in MVC for that we are using 'Range' attribute. In the Range attribute we could specifies minimum and maximum value for a numerical value.

The first parameter to the attribute is the minimum value, and the second parameter is the maximum value. The values are inclusive. The Range attribute can work with integers, doubles, dates, etc. We can specify Type in overloaded version constructor.

Here is example for this.