better way of showing File Upload Errors?

Posted by coure06 on Stack Overflow See other posts from Stack Overflow or by coure06
Published on 2010-04-25T09:32:01Z Indexed on 2010/04/25 9:33 UTC
Read the original article Hit count: 188

Filed under:
|

Model:
public class EmailAttachment
{
public string FileName { get; set; }
public string FileType { get; set; }
public int FileSize { get; set; }
public Stream FileData { get; set; }
}
public class ContactEmail: IDataErrorInfo
{
public string Name { get; set; }
public string Email { get; set; }
public string Message { get; set; }
public EmailAttachment Attachment { get; set; }

    public string Error { get { return null; } }  

    public string this[string propName]  
    {
        get  
        {  
            if (propName == "Name" && String.IsNullOrEmpty(Name))  
                return "Please Enter your Name";  
            if (propName == "Email"){  
                if(String.IsNullOrEmpty(Email))  
                    return "Please Provide an Email Address";  
                else if(!Regex.IsMatch(Email, ".+\\@.+\\..+"))  
                    return "Please Enter a valid email Address";  
            }  

            if (propName == "Message" && String.IsNullOrEmpty(Message))  
                return "Please Enter your Message";  
            return null;  
        }  
    }}    

And my controller file
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Con(ContactEmail ce, HttpPostedFileBase file)
{
return View();
}

Now the Problem
From the form i am getting Name,Email, Message and uploaded file. I can get validation errors automatically for Name,Email,Message using public string this[string propName]. How can i show validation errors if Attachment.FileSize > 10000? If i write its code in
public string this[string propName]
i alwasy getting Attachment null. How can i fill Attachment Object of ContactEmail so that i can manage all errors on same place?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about c#