Can EditorFor() be used to create <input type=“file”>?

前端 未结 4 662
名媛妹妹
名媛妹妹 2020-12-16 11:16

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,

4条回答
  •  半阙折子戏
    2020-12-16 11:23

    Here's an example for MVC 5 (required for the htmlAttributes).

    Create this as a file called HttpPostedFileBase.cshtml under ~\Views\Shared\EditorTemplates

    @model HttpPostedFileBase
    @{
        var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
        htmlAttributes["type"] = "file";
    }
    @Html.TextBoxFor(model => model, htmlAttributes)
    

    This generates the control with the correct id and name and works when editing collections from a model EditorFor template.

提交回复
热议问题