Creating Custom Validation using IValidatableObject (DataAnnotation) in ASP.NET MVC

.NET Framework provides a set of inbuilt data validation using DataAnnotation. 

But there might be cases where you might need to validate with your custom rules. Example of such case is like when you need to compare two values and one must be greater than (or less than another).

Here’s how you do it. 

You can change your Validate Function as per your need.
public class ViewModel: IValidatableObject
{
    [Required]
    public DateTime StartDate { get; set; }
    [Required]   
    public DateTime EndDate { get; set; }
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
       if (EndDate < StartDate)
       {
           return new ValidationResult(“EndDate must be greater than StartDate”);
       }
    }
}
Happy Coding!

Downloading Multiple Files as Zip Using Ionic.ZIp in ASP.NET MVC

We might come across a situation where we need to download multiple files and since a response cannot contain more than one file result; the best way to do is to zip all the content and download the zip file. 
Here's how we do it:
We need Ionic.Zip.Dll

We need 

using System.IO;
using Ionic.Zip;

 byte[] zipContent = null;
            using (var zip = new ZipFile())
            {
                zip.AddFile(Server.MapPath("~/Ionic.Zip1.dll"));
                zip.AddFile(Server.MapPath("~/Ionic.Zip2.dll"));

                //assign all zip content to content
                using (var ms = new MemoryStream())
                {
                    zip.Save(ms);
                    zipContent = ms.ToArray();
                }
            }
            Response.AppendHeader("Content-Disposition"string.Concat("inline; filename=\"""download.zip""\""));
            return File(zipContent, "zip");