Saving images with model and partial view key

MHarris 1 Reputation point
2022-03-29T12:35:49.95+00:00

I am creating what is essentially a diary where the main view is a vertical list within 'beginform' and underneath are several partial views with the textboxes in rows. The main view is information about a day and the partial view in question is events that happened that day. All data textbox is added to the db just fine.

I have already implemented image uploading and displaying for the main view. It works fine. I have now added a button at the end of each row in the partial view (additional rows are added with javascript) and I would like the user to be able to upload multiple images per row and later display them in the 'details' view. When the submit button is pressed on the form, the images are sent to the controller within the httppostedfilebase I have declared but I cannot get them added to the table and inherit the key of the partial view record.

So in my actionresult, I have (PersonsDiary PersonsDiary, List<TheEvents> TheEvents, HttpPostedFileBase DFiles, HttpPostedFileBase EFiles)

DFiles is the images for the day (main view), which I add to the db by looping through Request.Files. EFiles contains the images that are added for the event (partial view).

In my viewmodel, TheEvents contains ICollection<EventImages> EventImages.

When debugging, I can see TheEvents has EventImages within it but its count is zero. I need to get EFiles added to TheEvents with each array of files inheriting the key of the respective record. I think I should be able to have the images passed from the view as part of the List of TheEvents rather than add them within a loop in the controller, but if I do need to add them in the controller I can't figure out how to do it so they inherit the key of the record in the partial view.

Any pointers are appreciated.

Controller:

        public ActionResult Create(PersonsDiary PersonsDiary, List<TheEvents> TheEvents, List<WeatherDetails> WeatherDetailsInfo, List<WeatherObservations> WeatherObservationsInfo, HttpPostedFileBase[] DFile, HttpPostedFileBase[] EFile)
        {
            if (ModelState.IsValid)
            {

                List<DImages> DImagesList = new List<DImages>();
                List<EImages> EImagesList = new List<EImages>();
                Guid qID = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.CustomerGuid;
                if (DFile != null || EFile != null)
                {
                    for (int i = 0; i < Request.Files.Count; i++)
                    {

                        var fl = Request.Files[i];

                        if (fl != null && fl.ContentLength > 0)
                        {
                            var fileName = Path.GetFileName(fl.FileName);

                            string pic = System.IO.Path.GetFileName(fl.FileName);
                            string fpath = Server.MapPath("~/Content/images/profile/AllImages/" + qID + "/" + PersonsDiary.Location + " - " + PersonsDiary.Date_From.Value.Year + "-" + PersonsDiary.Date_From.Value.Month + "-" + PersonsDiary.Date_From.Value.Day.ToString("00") + "/");
                            string rpath = fpath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
                            bool exists = System.IO.Directory.Exists(fpath);
                            if (!exists)
                                System.IO.Directory.CreateDirectory(fpath);
                            // file is uploaded
                            fl.SaveAs(fpath + pic);
                            PDImages PDImage = new PDImages()
                            {
                                FileName = pic,
                                ImagePath = rpath + pic,

                            };

                            DImagesList.Add(PDImage);
                            TheEvents[i - 1].EventImages.Add(PDImage)


                        }

                    }
                    PersonsDiary.DImages = DImagesList;
                    //db.PersonsDiary.Add(PersonsDiary);
                }


                var EList = TheEvents.ToList();

                foreach (var detail in TheEvents)
                {

                    if (detail.Qty != null)
                    {
                        //detail.ID = id1;
                    }
                    else
                    {
                        EList.Remove(detail);
                    }

                }
                PersonsDiary.AnEvent = EList;

                var WeatherDetailsList = WeatherDetailsInfo.ToList();
                foreach (var WeatherDetailsTime in WeatherDetailsInfo)
                {
                    if (WeatherDetailsTime.Sun_Date != null)
                    {
                        //WeatherDetailsTime.sessionid = id1;
                    }
                    else
                    {
                        WeatherDetailsList.Remove(WeatherDetailsTime);
                    }

                }
                PersonsDiary.WeatherDetails = WeatherDetailsList;


                db.PersonsDiary.Add(PersonsDiary);

                db.SaveChanges();
            }

            return RedirectToAction("Index");

        }

View:
Is a form with the partial view loaded with RenderAction.
textboxes are all in a table
with the button to select images at the end of each row:

            <input type="file" name="EFile" id="EFile" multiple="multiple" style="width: 100%;" />
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,452 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.