locked
Transfering references via Actions RRS feed

  • Question

  • User-1343662111 posted

    Hey,

    I'm trying to upload videos to my Server via 3 Pages. The first page should contain the file upload and the title of the video and the second page

    contains more information about the video to fill out.  The third page is just a confirmation that the video was uploaded. 

    My problem is that i need to access the file i uploaded on the first page to access it's name after i sent the form on the second page. Transfering the data from 

    page 1 to 2 works perfectly fine but when i go to 3 all data from 1 gets deleted and i can only access the data of 2.

            //Page 1
            public ViewResult Upload()
            {
    
                var viewModel = new UploadViewModel();
    
                return View("Upload", viewModel);
            }
    
            //Page2
            [HttpPost]
            public ActionResult Upload(UploadViewModel model)
            {
               
                if (model.File != null)
                {
                    model.Filename = model.File.FileName;
                    string internPath = "Media\\" + model.File.FileName + "ordner";
                    string filePath = Server.MapPath("~") + internPath;
                    Directory.CreateDirectory(filePath);
                    model.File.SaveAs(filePath + @"\Video.mp4");
                    model.Thumbnails = ThumbnailConverter.GetImage(filePath + @"\Video.mp4", filePath, internPath);
                }
                return View("Upload", model);
            }
    
            //Page3
            [HttpPost]
            public ActionResult UploadFinished(UploadViewModel model)
            {
            
                int thumbnailIndex = 0;
                string est = Request.Form["thumbString"];
    
                if (Int32.TryParse(est, out thumbnailIndex))
                {
                    if (thumbnailIndex < 0)
                    {
                        string internPath = "Media\\" + model.File.FileName + "ordner";
                        string filePath = Server.MapPath("~") + internPath;
                        Directory.CreateDirectory(filePath);
                        model.File.SaveAs(filePath + @"\thumbnail.png");
                        model.ThumbnailPath = internPath + @"\thumbnail.png";
                    }
                    else
                    {
                        model.ThumbnailPath = model.Thumbnails[thumbnailIndex];
                    }   
                }

    I thought handing over the viewmodel the data is saved in would do the trick but it seems like in the .cshtml form a new viewmodel gets created and it just uses the values of the Model i created the view with.

    How do i make the form to remember the File reference and pass it on in the form without using another textbox (type file)?

    Thursday, November 22, 2018 10:38 AM

All replies

  • User323983933 posted

    Not sure if this is the cause, but my postbacks look like 

           [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "Contact,Address2,Address3,Town,PostCode,Country,TelephoneNo,Fax,eMail,Mobile,EmergencyContactNo")] Address address)
    

    The Bind(Include tells the post what data to accept.

    So in the first postback, you're just reloading the same page, you're not redirecting to another page, like Response.redirect("upload","UploadFinished")  You can store the UploadViewModel in the session or ViewData if you want it to persist.  I'm pretty sure ViewData is not blanked on a redirectToAction.

    Friday, November 23, 2018 10:22 PM