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.
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.
public ActionResult Create(PersonsDiary PersonsDiary, List<TheEvents> TheEvents, List<WeatherDetails> WeatherDetailsInfo, List<WeatherObservations> WeatherObservationsInfo, HttpPostedFileBase DFile, HttpPostedFileBase EFile)
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);
// file is uploaded
fl.SaveAs(fpath + pic);
PDImages PDImage = new PDImages()
FileName = pic,
ImagePath = rpath + pic,
TheEvents[i - 1].EventImages.Add(PDImage)
PersonsDiary.DImages = DImagesList;
var EList = TheEvents.ToList();
foreach (var detail in TheEvents)
if (detail.Qty != null)
//detail.ID = id1;
PersonsDiary.AnEvent = EList;
var WeatherDetailsList = WeatherDetailsInfo.ToList();
foreach (var WeatherDetailsTime in WeatherDetailsInfo)
if (WeatherDetailsTime.Sun_Date != null)
//WeatherDetailsTime.sessionid = id1;
PersonsDiary.WeatherDetails = WeatherDetailsList;
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%;" />