简体   繁体   中英

Retain value of input type file in a ASP.Net MVC view after page refresh

I am using a input type file in MVC view form whose value is posted to action method for performing some validation. The input file value is binded to a property in model which will be returned after page refresh. But file value is not getting binded automatically after page refresh.Please find below the code.

This is my view code.

@model WebApplication10.Models.Model1

<h2>HtmlToPDF</h2>
@using (Html.BeginForm("HtmlToPDF", "Home", FormMethod.Post, new { enctype = 
"multipart/form-data" }))
{
    <table>
        <tr>
            <td>File</td>
            <td><input type="file" name="File1" value="@Model.File1"/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="ToHTML" /></td>
        </tr>
    </table>
}

Controller code:

[HttpPost]
    public ActionResult HtmlToPDF(Model1 file)
    {
        //Some validation.
        return View(file);
    }

and this is my model:

public class Model1
{
    public HttpPostedFileBase File1 { get; set; }
}

Any help will be appreciated.

  1. Due to security reason browser will not allow you to set that information. Once Form Submit input of type=file get cleared and it is not set manually by script or other programming stuff.

  2. In your case you can do something like this. When Form Is Submitted , you can hold Model1 property File1 information in some other object. Like FileName , FileSize and StreamOfData. You pass marker to Form that you have stored that data. So when next time Form submitted you can consider that data if any new data is not passed.

I hope this suggestion will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM