This is a migrated thread and some comments may be shown as answers.

[Solved] Upload tool for ASP.NET MVC on Postback

3 Answers 61 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andrew
Top achievements
Rank 1
Andrew asked on 22 Mar 2011, 07:03 PM
I am currently using the upload tool for ASP.NET MVC, and am having a small issue. Part of the logic when the page posts is to make sure that a file exists and that the user has input a name for the record. If there is no name entered, the model state become invalid and the view is reloaded with the information from the model. My problem is, if you select a file to upload, forget to enter a name, then submit the form, the validation fails and sends you back to the page as it should, but the list of attachments to upload is not retained. Is there any way I can retain these attachments and pass them back to the view if the model is invalid? 

3 Answers, 1 is accepted

Sort by
0
T. Tsonev
Telerik team
answered on 23 Mar 2011, 11:08 AM
Hi Andrew,

The list of uploaded files can't be retained between requests. What you can do is to store the file in a temporary location and use it on the next form submit. The temporary location can be transferred between views using TempData.

We can try to provide a detailed suggestion if you share more details about your scenario.

I hope this helps.

Best Regards,
Tsvetomir Tsonev
the Telerik team
0
Andrew
Top achievements
Rank 1
answered on 23 Mar 2011, 03:59 PM
Thanks for your quick response!

Basically what I have going on is this: There is a "Create Process" page, which allows a user to create a process consisting of a name (string), description (string), and a file. The name and file are both required attributes of a process. If a user selects a file to upload, but fails to enter a name, the validation will fail (as name is required). When the validation fails, the view is reloaded, but the file that they attached is lost. The code below is what happens when the create view posts:

[HttpPost]
public ActionResult Create(EditProcessViewModel processToCreate, IEnumerable<HttpPostedFileBase> attachments)
{
    //Make sure the user selected a file to upload
    if (attachments == null)
    {
        ModelState.AddModelError("path", "You must select a file to upload");
    }
  
    //If the model is not valid, reload the page with the appropriate errors
    if (!ModelState.IsValid)
    {
        return View(processToCreate);
    }
    else
    {
        //Code to save the uploaded file and persist the process to the database
    }
}

Basically what I want to happen is not only preserve the "EditProcessViewModel", but the file that the user chose to upload as well, so that when the user is returned to the view because of a failed validation, the file they chose is still in the attachments list.
0
Atanas Korchev
Telerik team
answered on 24 Mar 2011, 11:10 AM
Hello Andrew, 

It seems that the previous suggestion is the only possible way:
  1. Always save the uploaded files 
  2. Store the location (path) of the saved files in TempData or Session
  3. If validation fails show the user the uploaded files - you can even hide the upload if there are any uploaded files stored in TempData

Regards,
Atanas Korchev
the Telerik team
Tags
General Discussions
Asked by
Andrew
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
Andrew
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or