Given this model, is it possible to use the Html.EditorFor() to render a file upload input element to the page? I played around with the Datatype of the property FileName, and it was definitely impacting the editor form rendered.
public class DR405Model
{
[DataType(DataType.Text)]
public String TaxPayerId { get; set; }
[DataType(DataType.Text)]
public String ReturnYear { get; set; }
public String FileName { get; set; }
}
Strongly Typed *.aspx page looks like this
<div class="editor-field">
<%: Html.EditorFor(model => model.FileName) %>
<%: Html.ValidationMessageFor(model => model.FileName) %>
</div>
Add: htmlAttributes = new { type = "file" }
Note: I'm using MVC 5, I have not tested on other versions.
Here's an example for MVC 5 (required for the htmlAttributes).
Create this as a file called HttpPostedFileBase.cshtml under ~\Views\Shared\EditorTemplates
This generates the control with the correct id and name and works when editing collections from a model EditorFor template.
It would make more sense to use HttpPostedFileBase to represent an uploaded file on your view model instead of
string
:then you could have the following view:
And finally define the corresponding editor template inside
~/Views/Shared/EditorTemplates/HttpPostedFileBase.ascx
:Now the controller might look like this:
No but take a look at http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx