0

Been new to VB .Net MVC (v5.0) and Razor (v3.0) I've made my way so far through the subtlety of out of the box client side unobtrusive validation tools.

But now I guess I need to go custom as I can't find anywhere a straightforward solution to my problem.

So here is the ViewModel that is concerned :

Public Class ContactViewModel

   <Required>
   Public Property MailAddress As String

   <Required>
   Public Property MailObject As String

   <Required>
   Public Property MailBody As String

   <AllowedFileExtentions("doc", "docx", "xls", "png", "jpeg", "pdf", "xlsx", "odt")>
   <MaxTotalFileSize(1024 * 1024 * 10, "Total file size exceeded authorised limit of 10 Mo")>
   <MaxFileSize(1024 * 1024 * 5)>
   Public Property AttachementFiles As List(Of HttpPostedFileBase)

End Class

As you can see, I've already written (but not yet tested) custom validation attributes for the AttachementFiles property according to this blog. The goal of each of those attributes should be obvious according to there names ;)

My trouble here is that I was on my way to implement the corresponding client side javascript code rules when I've noticed I had no clues how to have .Net MVC integrating everything all together in a smooth way.

Which helper function should I call in my view in order to have an <input type="file" multiple/> generated with the right data-val- attributes for the unobtrusive part ?

I remember seeing here and there it should be possible to define custom template for List(Of HttpPostedFileBase) type in order to have EditorFor helper generating what I want, but I can't help wondering if there is not a better (simpler in fact :)) way to achieve that.

Are they helper functions existing (according to my MVC / Razor version constraints) that would spare me the writing of the custom template ?

If there isn't, what is your advise in order to be as code straightforward as possible ?

Please notice : I'm aware that such validation on client side (file size and extension) is dependent on browser capabilities, but I'm fine with it and my real concern is the way to glue everything together with the least effort possible (after all, a good dev is lazy by nature ;))

Thanks in advance for any help !

Lemmy
  • 291
  • 2
  • 12
  • Refer also [File upload, Client script not called ? Data-annotation](http://stackoverflow.com/questions/39470135/file-upload-client-script-not-called-data-annotation/39481203#39481203) for how to generate the input and a link to a `FileSizeAttribute`. And you do not need to _define custom template_ and _validation on client side_ is **not** dependant on the browser (you just need the relevant scripts) –  Sep 20 '16 at 13:51
  • @StephenMuecke : Thanks for your feedback ! I've been googling about it for two days with no success. Never thought a TextBoxFor would do the trick been so so convinced that there should already be an helper function for that. By _validation on client side is dependant on the browser_ I meant it's not supposed to be possible if the browser do not support the HTML5 files capabilities (accessing file name / file size ...) I agree, it should get close to 0,01% of browsers now days, but who knows ;) – Lemmy Sep 20 '16 at 14:58
  • @StephenMuecke : By the way, I'm wondering the following even if it seems to be overkill : Is it possible to have both an accept field fed from my validation attribute as well as client side unobtrusive jQuery validation without having a dedicated template to achieve that ? I guess my question must summarize to : is it possible to add custom attributes (accept in that case) during the process of `TextBoxFor` – Lemmy Sep 20 '16 at 15:18
  • You can always add the `accept` attribute (in the same way you add the `type` attribute - `new { type="file", accept="pdf" }` but to do it based on your attribute, you will need create your own `HtmlHelper` extension method. –  Sep 20 '16 at 22:03

0 Answers0