Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and EditorFor FileUpload in asp.net mvc 3 razor?

The lack of an EditorFor file in asp.net mvc 3 seems like such a glaring omission I wonder: Is there some way that mvc handles file uploads that is just not publicized that well? As near as I can tell there is no built in way to handle file uploads.

I'm just curious if the file upload capability is in fact there and I'm just missing it, or if it does not exist at all.

like image 210
Steve French Avatar asked Mar 18 '11 21:03

Steve French


2 Answers

This works great for me, provides client-side validation too.

CSHTML:

<div class="editor-label">
    @Html.LabelFor(model => model.Image)
</div>

<div class="editor-field">
    @Html.TextBoxFor(model => model.Image, new { type = "file" })
    @Html.ValidationMessageFor(model => model.Image)
</div>

Model:

[Required("Image is required")]
public HttpPostedFileBase Image { get; set; }
like image 137
Travis Weber Avatar answered Oct 06 '22 19:10

Travis Weber


No, but the following steps don't seem like too much work to me. Not to mention that you could write a custom editor template which will render a file input for a given property.

like image 20
Darin Dimitrov Avatar answered Oct 06 '22 21:10

Darin Dimitrov