I have a file upload page which on button click I am calling one method where I define the maximum size to be 5 MB. But when I upload more than that, the size error message is not displaying. Here is my code:
public ActionResult document(HttpPostedFileBase file, Document model)
{
    tbldocument doc = new tbldocument();
    //Document model = new Document();
    doc.DocumentName = model.documentname;
    if (ModelState.IsValid)
    {
        if (file == null)
        {
            ModelState.AddModelError("File", "Please Upload Your file");
        }
        else if (file.ContentLength > 0)
        {
            int MaxContentLength = 1024 * 1024 * 10; //10 MB
            string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".pdf", ".docx", ".doc", ".xml", ".odt", ".xlsx", ".ppt" };
            if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
            {
                ModelState.AddModelError("File", "Please file of type: " + string.Join(", ", AllowedFileExtensions));
            }
            else if (file.ContentLength > MaxContentLength)
            {
                ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB");
            }
            else
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Server.MapPath("~/App_Data/uploads/documents/" + model.projectid + "");
                //var pathFile = path + fileName;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                var pathWithfileName = Path.Combine(path, fileName);
                file.SaveAs(pathWithfileName);
                ModelState.Clear();
                tbldocument objdb = new tbldocument();
                objdb.DocumentName = model.documentname;
                objdb.ProjectId = model.projectid;
                objdb.Path = model.path;
                objdb.Path = Convert.ToString(pathWithfileName);
                // objdb.FileName = file.FileName;
                //objdb.FileName = file.FileName;
                objdoc.upload(objdb);
                //" + projectid + "
                ViewBag.Message = "File uploaded successfully. File path : ~/Upload/" + fileName;
            }
        }
    }
    return RedirectToAction("ProjectTask", "TblTask");
}
Here is the snap  . Instead of showing the message to the user that the file must be smaller than 5 MB, it throws an error like:
. Instead of showing the message to the user that the file must be smaller than 5 MB, it throws an error like:

 
     
     
    