1

I'm trying to validate a form using Data Annotation. It seems great for string types and integers, but for a file upload, I couldn't validate from the class. It would just be sent a string "HttpPostedFileWrapper". Does anyone have any tips?

Thanks

1
  • Any chance that you could provide some example code? Commented Mar 9, 2010 at 10:03

1 Answer 1

4

You can just use data annotations as per general usage.

For example, a viewmodel such as:

public class UpdateSomethingViewModel {
    [DisplayName("evidence")]
    [Required(ErrorMessage="You must provide evidence")]
    [RegularExpression(@"^abc123.jpg$", ErrorMessage="Stuff and nonsense")]
    public HttpPostedFileWrapper Evidence { get; set; }
}

Then in your controller just the usual:

[HttpPost]
public ActionResult UpdateSomething(UpdateHSomethingViewModel model)
{
    if (ModelState.IsValid)
    {
        // do stuff - plenty of stuff
        // weee, we're off to see the wizard.

        return RedirectToAction("UpdateSomethingSuccess", model);
    }

    return View(model);
}

I've just tested (albeit in MVC2/.net 4) and it worked a treat.

Hope that helps.

Cheers, Terry

Sign up to request clarification or add additional context in comments.

1 Comment

HttpPostedFileWrapper is just what I have been looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.