I am using DataAnnotationsExtensions from http://dataannotationsextensions.org/
with an example from here http://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx
Controller Code
public ActionResult Create(Dog dog, HttpPostedFileBase Picture)
{
Regex rgx = new Regex(@"^.*\.(jpg|gif|jpeg|png)$");
Match m = rgx.Match(Picture.FileName);
if (rgx.IsMatch(Picture.FileName))
{
if (ModelState.IsValid)
{
Model Code
[FileExtensions("png|jpg|jpeg|gif", ErrorMessage = "Only jpg jpeg gif or png files allowed")]
public string Picture { get; set; }
and the razor code
@Html.TextBoxFor(model => model.Picture, new { type = "file" })
@Html.ValidationMessageFor(model => model.Picture)
Everything I do it fails everytime at
if (ModelState.IsValid)
If I remove the FileExtensions Annotation it works fine but then I no longer have the ability to block file types that I dont want.
I have gone as far as to check the code for the data annotations extentions located here
https://github.com/srkirkland/DataAnnotationsExtensions/blob/master/DataAnnotationsExtensions/FileExtensionsAttribute.cs
and I still cant seem to find out where the problem is.
I believe that the model binder is getting confused with a property named ‘Picture’ on your model and a
HttpPostedFileBaseparameter named ‘Picture’ as well.If you update your
HttpPostedFileBaseparameter to something other than ‘Picture’, the model binder should behave normally. Example: