Posts

ASP.Net MVC : Conditional Validation using ValidationAttribute

In ASP.Net MVC we can validate each input field by decorating the corresponding property in the model class with some validation attributes like Required , MaxLength , MinLength ,...etc. Sometime we need to validate a property according to another property value for example if we have a registration form and we need the user enter his age if he is a male and age not required if the user sex is female. So if we decorated the Age property with Required attribute it will show error message even if the user is female, so we need to bypass this check with females. Assume our registration form will contains three fields, name, sex and age, and its model will be as follows public class RegisterationModel { [Required(ErrorMessage = "*")] public String Name { get; set; } [Required(ErrorMessage = "*")] [Display(Name = "Gender")] public Sex Sex { get; set; } [RequiredIf("Sex", Sex.Male, "enter your age")] pu...

Restrict non-ajax requests using ActionMethodSelectorAttribute

In ASP.Net MVC each coming request is routed to a certain action method inside some controller and with JQuery it became piece of cake to call this action method using ajax. In some scenarios we need to restrict the incoming requests to be ajax request only, fortunately ASP.Net MVC represent a new method to Request object called IsAjaxRequest() that return true if the current request come from ajax call and return false if current request is normal request(typing url in address bar of a browser, click a link to this page,..etc). We can use that method to deny any request come from ajax call or vise versa. public class HomeController { public ActionResult Index() { if(Request.IsAjaxRequest()) { //Do something } else { //Do something else } } } As we see if we need to make this check many times in our project, we will rewrite the same if statement for each action we need it be called via ajax only, but is there anot...

How to create android application?

Image
We have already showed how to prepare your machine to develop your android application  now it is the time to check if we prepared our machine successfully and we can write our first android application or not. We will start learning developing android application by writing a simple application that will display "Hello World" sentence (the most famous example to start learning new tech). Follow the next steps to create your first android application: Lunch Eclipse and select File > New > Project you will find a list of available projects templates select  android project template then click next, you will see window like this   Application Name : the name of your applications that will be appear on application list of your device(mobile or tablet) Project Name : the name that will be appear in your eclipse work-space. Packaeg Name : it will be used as identifier for your application and must be unique across all applications. Build SDK : specify...

Fundamental components of android application

This post is a one of some posts about android platform I will write to explore this platform and learn you(and me) developing android applications for mobiles and tablets devices. You don't have to own android mobile to create android application, what is you really need is a tool (Emulator) to test and debug your application. You can refer to this post  where I showed how to prepare your machine to be able to create your android application. That post was written before releasing Ice Cream Sandwich  version (android 4.x) but it does not matter, just you should download the latest version of all tools you have to install. My reference for writing these posts is Pro Android 4 book. In this post we will explore the key components of android framework which we must understand before delving into coding process. View: views are user interface(UI) elements such as buttons, labels, text fields, or any other UI element. Views also can contain other views. Anyth...

Load partial page in jquery and ASP.Net MVC

In this post I will show you how much ASP.Net MVC and JQuery are making up a dangerous duet and how you can do big tasks in simple way with little efforts. We can take a look at a live example of what we will do in this post. In stackoverflow website specially users page and tags page you will find a text box to search users/tags. the page is loaded with some users info sorted with default setting. if you start to write something in that text box, the users collection will be filtered by the name you entered in ajax manner and ordered alphabetically(ascending). The issue here is how using the same piece of code that produce the whole page when it be loaded in first time to filter the users when you type some letters in the text box and update just the part that display the users data. Here the magic of ASP.Net MVC and  JQuery  come to the scene. Lets start to build our page first then start to filter our users collection. I will not show you how to cr...

Generic CheckBoxList in ASP.Net MVC

In ASP.Net  web forms you can easily create a list of check boxes and bind it into a data collection using CheckBoxList control and specify which property will be the text of each check box and which one will be the value of the check box. You even don't need to write it, you can drag it from tools window, drop it into your page and set its properties and style with wizards that Visual studio provide. But in ASP.Net MVC there is no any server controls like that exists in web forms, all what we have is a helper methods to create HTML tags. In  ASP.Net MVC we have two helpers methods for creating a check box, Html.CheckBox and Html.CheckBoxFor that will be rendered as just one check box. If we want to create many check boxes or check boxes list we can call it inside a loop as follows @foreach (var item in MyCollection) { @Html.CheckBox("checkBoxName", new {value=item.checkBoxValue })@item.CheckBoxText<br /> } Here is a generic extension method for creat...

Generic Lambda Expression to sort collections of different types

Suppose you have many collections with different types that you want to sort, of course you can do it by OrderBy or OrderByDescending extension methods but you have to specify the order criteria every time you call these methods. If the order criteria is determined in runtime, then you have to build a separate function to each order criteria of each type. To clear what I want to tell you suppose you have two classes,  Employee and Department . public class Employee { public String Name { get; set; } public Int32 Age { get; set; } public DateTime HiringDate { get; set; } } public class Department { public String DepartmentName { get; set; } public String Code { get; set; } } And you want to order the employee collection by one of its properties (Name,Age,or HiringDate) and order department collection by one of its properties. OrderBy and OrderByDescending methods need Func<TSource,TKey> as selector to compare the collection items and order them....