Create Edit Field for HttpPostedFileBase type in mvc Edit.cshtml view?

When I create new item and add image by input type file it works ok. But I have this same code in Edit.cshtml and I want to set input type file from Model and id but it doesn't work. I cannot set this input file.

Asked by:- Nguyễn ThànhThịnh
: 4819 At:- 3/17/2018 8:27:13 PM
asp net mvc mvc5 HttpPostedFileBase

you can't create Edit for input type=file, follow @vipin's answer, this is the only way. 0
By : Sam - at :- 3/19/2018 8:09:15 AM

1 Answers
profileImage Answered by:- Vipin

You cannot create edit field of input type =file, instead what you can do is to show all the uploaded file or files in Edit.cshtml and allow users to delete uploaded file or add new one in the Model.

So your view must be like


So in this way you can add/remove new file from your Model, and show you are showing current linked file of the model also.

So, consider if your Model is

public class Support
       public int SupportId { get; set; }
       [Required(ErrorMessage = "Please Enter Your Name")]
       [Display(Name = "Name")]
       public string Name { get; set; }
       [Required(ErrorMessage = "Please Enter Summary")]
       [Display(Name = "Summary")]
       public string Summary { get; set; }
       public virtual ICollection<FileDetail> FileDetails { get; set; }

And FileDetails class

public class FileDetail
       public Guid Id { get; set; }
       public string FileName { get; set; }
       public string Extension { get; set; }
       public int SupportId { get; set; }
       public virtual Support Support { get; set; }

then your edit View will be

@model MultipleFileUpload.Models.Support
    ViewBag.Title = "Edit";
@using (Html.BeginForm("Edit", "Support", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
        <legend>Edit Support Request</legend>
        @Html.HiddenFor(model => model.SupportId)
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        <div class="editor-label">
            @Html.LabelFor(model => model.Summary)
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Summary)
            @Html.ValidationMessageFor(model => model.Summary)
        <div class="editor-label">
        <div class="editor-field">
            <input type="file" name="file" multiple="multiple" />
            <ul class="attachment">
                @foreach (var item in Model.FileDetails)
                        <a class="title" href="/Support/Download/?p=@(item.Id + item.Extension)&d=@item.FileName">@item.FileName</a>
                        <a href="javascript:void(0);" data-id="@item.Id" class="deleteItem">X</a>
            <input type="submit" value="Save" />

Considering your above View and Model, you will get the Edit.cshtml as shown in first image.

Reference :

At:- 3/18/2018 6:39:39 AM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use