asp.net mvc 4 - Can't get MVC 4 FileExtensions Attribute to work on ViewModel property -



asp.net mvc 4 - Can't get MVC 4 FileExtensions Attribute to work on ViewModel property -

i need upload csv file , want restrict it's extension .csv

so added follow property viewmodel:

[fileextensions(errormessage = "must take .csv file.",extensions = "csv,txt")] public httppostedfilebase file { get; set; }

in view have following:

@html.textboxfor(m => m.file, new { type = "file"}) @html.validationmessagefor(m => m.file)

however hits "modelstate.isvalid" check returns invalid error message of "must take .csv file."

i assume i'm missing parameter, haven't found sample of in utilize yet.

the problem fileextensionsattribute works on string variables. easiest way check file extension of httppostedfilebase variable utilize this simple attribute. solved problem.

the downside new attribute validated on serverside don't forget check model state with:

if (modelstate.isvalid) { // work }

asp.net-mvc-4 data-annotations

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -