Click here to Skip to main content
15,918,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a web application developed in MVC. It's an existing application. I want to validate the excel file with data annotation. But When I was trying to add the data annotation namespace in model class. I can only see I am not able to find the data annotation under componentmodel namespace. Also HttpPostedFileBase is not working under system.web namespace. I removed the reference and added again but its not working. Please help me to solve this.

What I have tried:

using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotation;

namespace SMARTCON.Models
    public class UploadFileValidation
        //[ValidateFile(ErrorMessage = "Please select a PNG image smaller than 1MB")]
        //public HttpPostedFileBase File { get; set; }

        [Required(ErrorMessage = "Please select file to upload")]
        [RegularExpression(@"([a-zA-Z0-9\s_\\.\-:])+(.xls|.xlsx)$", ErrorMessage = "Only .Xlx files allowed.")]
        public HttpPostedFileBase PostedFile { get; set; }

[Required(ErrorMessage = "Please select file to upload")] this is coming as normal string only not as keyword
Updated 8-Jun-20 2:49am

1 solution

The namespace is System.ComponentModel.DataAnnotations - note the "s" on the end - and requires a reference to the System.ComponentModel.DataAnnotations assembly.

System.ComponentModel.DataAnnotations Namespace | Microsoft Docs[^]

HttpPostedFileBase is defined in the System.Web namespace, and requires a reference to the System.Web assembly.

HttpPostedFileBase Class (System.Web) | Microsoft Docs[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900