Beispiel #1
0
        public async Task <IViewComponentResult> InvokeAsync(int storeId)
        {
            var user = await GetCurrentUserAsync();

            // create a new StoreNoteViewModel
            StoreNoteViewModel snvm = new StoreNoteViewModel()
            {
                // attach the current store id to the view model, to use to attach to a new store note when one is created
                CurrentStoreId = storeId,
                CurrentUser    = user
            };

            // get StoreNotes from the database for the current store being viewed
            snvm.AllNotesForStore = await _context.StoreNote.Include("User").Include(sn => sn.Store).Where(sn => sn.Store.StoreId == storeId).OrderBy(sn => sn.DateCreated).ToListAsync();

            // return notes
            return(View(snvm));
        }
Beispiel #2
0
        public async Task <IActionResult> Create(StoreNoteViewModel storeNoteViewModel)
        {
            // attach store Id from view model to the new StoreNote
            storeNoteViewModel.StoreNote.StoreId = storeNoteViewModel.CurrentStoreId;

            ModelState.Remove("StoreNote.User");

            if (ModelState.IsValid)
            {
                // get current user
                var user = await GetCurrentUserAsync();

                storeNoteViewModel.StoreNote.User = user;

                _context.Add(storeNoteViewModel.StoreNote);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Details", "Stores", new { id = storeNoteViewModel.StoreNote.StoreId }));
            }
            ViewData["StoreId"] = new SelectList(_context.Store, "StoreId", "Name", storeNoteViewModel.StoreNote.StoreId);
            return(RedirectToAction("Details", "Stores", new { id = storeNoteViewModel.StoreNote.StoreId }));
        }