Question

I have a ViewModel:

    public HttpPostedFileBase File { get; set; }
    public string Notes { get; set; }

In the Create view, I have an <input type="file" name="file" /> that binds the HttpPostedFile to the model. So far, so good.

My problem begins when I try to develop the "Edit" action with their respective view. I haven't problems at all to show the editor field for the string property Notes, that part is easy.

However, how do I show the <input type="file" name="file" /> with the actual value in the Edit view? How do I to bind a byte array to the File property of the ViewModel? Is that the correct way? Is there another better solution? I'm stranded with this and would appreciate your help.

Was it helpful?

Solution

You can't. An <input type="file" /> is not made for editing files, just for uploading them.

If you just want to be able to delete the file in the edit view try this (just an example):

  1. Add a bool property DeleteFile to the edit view model
  2. Add that property as a checkbox in the edit view
  3. In your edit method check if DeleteFile is set to true and if so, find and delete the file attached to that record you are editing

And you could rename File to UploadedFile (so its a bit clearer). But thats up to you of course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top