Question

Can someone help me with model validation on a HttpPostedFileWrapper object?

Model:

[Required(AllowEmptyStrings = false)]
public HttpPostedFileWrapper BlahFile { get; set; }

Controller:

   [HttpPost]
    public ActionResult LoadBlahData(BlahModel blahModel) 
    {
        if (!ModelState.IsValid)
            return RedirectToAction("Index");
    }

cshtml:

@using (Html.BeginForm("LoadBlahData", "Admin", FormMethod.Post, new { @class = "blahhForm", enctype = "multipart/form-data", id = "uploadBlah" }))
{
    <fieldset>
        <legend>Upload Blah Information</legend>
        @Html.LabelFor(x=>x.BlahFile, "Upload Blah file:")
        <input size="26" class="uploader" type="file" name="BlahFile" />
        <p><input class="ttButton" type="submit" value="Load Stuff" /></p>
    </fieldset>
}

Problem:

  1. Cannot see the "data-val*" attributes being added to the html.
  2. Does not set the unobtrusive validation off (red border on input box)

Notes:

  1. Other items in the Model are working fine with validation, its only the <input type="file"/> that seems to be having problems.
  2. Comes into the action method fine - (i.e - i can access the InputStream if i want).
  3. All scripts are referenced correctly (its working on normal text input's)

Thanks in advance,

Was it helpful?

Solution

Just for anyone else coming across this question you can also do this -

<%: Html.TextBoxFor(x => x.BlahFile, new { type = "file" }) %>

OTHER TIPS

you have not added any data attributes to your element. Add it like,

<input data-pk="1" size="26" class="uploader" type="file" name="BlahFile" />

and there is no support of validation of <input type="file"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top