MVC Model validation on “HttpPostedFileWrapper”
-
29-10-2019 - |
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:
- Cannot see the "data-val*" attributes being added to the html.
- Does not set the unobtrusive validation off (red border on input box)
Notes:
- Other items in the Model are working fine with validation, its only the
<input type="file"/>
that seems to be having problems. - Comes into the action method fine - (i.e - i can access the InputStream if i want).
- All scripts are referenced correctly (its working on normal text input's)
Thanks in advance,
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