public async Task<ActionResult> CultureCreatePartial(CultureCreatePostModel result) { //Parse the data and configure a culture model Culture model = new Culture(); foreach(TraitRank rank in result.Selection) { model.GetType().GetProperty(rank.PropertyName).SetValue(model, rank.Rank); } model.Id = Guid.NewGuid(); //Create a new Db context ApplicationDbContext context = new ApplicationDbContext(); //Store the model context.Cultures.Add(model); await context.SaveChangesAsync(); //Update the appropriate account switch (result.CreatorType) { case (CultureCreatorType.SeekerAccount): { SeekerAccount account = (from s in context.SeekerAccounts where s.Id == result.CreatorId select s).FirstOrDefault(); if (account == null) break; context.SeekerAccounts.Attach(account); var entry = context.Entry(account); entry.Reference(e => e.Culture).CurrentValue = model; await context.SaveChangesAsync(); return RedirectToAction("Index", "Seeker", new { area = "" }); } case (CultureCreatorType.JobPosting): { JobPosting account = (from s in context.JobPostings where s.Id == result.CreatorId select s).FirstOrDefault(); if (account == null) break; context.JobPostings.Attach(account); var entry = context.Entry(account); entry.Reference(e => e.Culture).CurrentValue = model; await context.SaveChangesAsync(); break; } default: { break; } } return null; }
// GET: Culture public ActionResult CultureCreatePartialJobPosting(JobPosting model) { Culture culture = new Culture(); List<Trait> listItems = new List<Trait>(); foreach (var prop in from s in culture.GetType().GetProperties() select s) { if (!prop.PropertyType.Equals(typeof(int))) continue; Trait item = new Trait { DisplayName = prop.Name, PropertyName = prop.Name }; listItems.Add(item); } CultureCreateViewModel viewModel = new CultureCreateViewModel { CreatorType = CultureCreatorType.JobPosting, CreatorId = model.Id, Traits = listItems }; return PartialView("CultureCreatePartial", viewModel); }