Search code examples
c#asp.net-mvcasp.net-coremodel-view

How do I retrieve a value from a object in an method to use it in another method?


I know that this question as been ask plenty of time but it does not seem to be working for me. Could you please help or provide me with a link with a similar question?

I have an object “ws” and I would like to capture the WorkShiftID so I can use it in CreateSharedView.

Ps: Sorry for my english, I am new to c# so please bear with me. Thank you

EDIT:I would like to retrieve ws.WorkShiftId from Create to put it in task.WorkShiftId in CreateSharedView. Is it possible?

        public IActionResult Create()
        {
            ViewData["HoleId"] = new SelectList(_context.Hole, "HoleId", "HoleName");
            ViewData["SupplierId"] = new SelectList(_context.Supplier, "SupplierId", "SupplierName");
            ViewData["SurveyLocationId"] = new SelectList(_context.SurveyLocation, "SurveyLocationId", "SurveyLocation1");
            ViewData["ZoneId"] = new SelectList(_context.Zone, "ZoneId", "ZoneName");

            return View();
        }

        // POST: WorkShiftDetailViewModels/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(WorkShiftDetailViewModel workShiftDetailViewModel)
        {

            if (!ModelState.IsValid)
            {
                WorkShift ws = new WorkShift();
                ws.StartDay = workShiftDetailViewModel.StartDay;
                ws.EndDay = workShiftDetailViewModel.EndDay;
                ws.SupplierId = workShiftDetailViewModel.SupplierId;
                ws.SurveyLocationId = 1;
                ws.ZoneId = workShiftDetailViewModel.ZoneId;
                ws.HoleId = workShiftDetailViewModel.HoleId;
                _context.Add(ws);
                await _context.SaveChangesAsync();

                foreach (WorkerViewModel member in workShiftDetailViewModel.WorkShiftEmployees)
                {
                    if (member.isDeleted == false) {
                        WorkShiftTeam emp = new WorkShiftTeam();
                        emp.EmployeeId = member.EmployeeId;
                        emp.RoleId = member.RoleId;
                        emp.WorkShiftId = ws.WorkShiftId;
                        test = ws.WorkShiftId;
                        _context.Add(emp);
                    }
                }

                return RedirectToAction(nameof(CreateSharedView));

            }

        public IActionResult CreateSharedView()
        {

            ViewData["TaskId"] = new SelectList(_context.Task, "TaskId", "TaskDescription");
            ViewData["WorkShiftTaskId"] = new SelectList(_context.WorkShift, "WorkShiftId", "WorkShiftId");

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> CreateSharedView(SharedViewModel sharedViewModel, 
                                                          SurveyViewModel surveyViewModel,
                                                          WorkShiftTask task)
        {
            if (ModelState.IsValid)
            {

                WorkShiftTask task = new WorkShiftTask();
                task.WorkShiftTaskId = sharedViewModel.WorkShiftTaskId;
                task.TaskId = sharedViewModel.TaskId;
                _context.Add(task);

                await _context.SaveChangesAsync();
            }
            return View(sharedViewModel);
        }```



Solution

  • The simpliest way is to use Session to store and get the ws.WorkShiftId,after configuring session state in your startup,

    In Create POST action:

    HttpContext.Session.SetInt32("wsId", ws.WorkShiftId);
    

    In CreateSharedView POST action:

    task.WorkShiftId = HttpContext.Session.GetInt32("wsId");
    

    Another more complicated way is that you could pass the ws.WorkShiftId as parameter when you redirect or use TempData:

    Create POST Action:

     //Or TempData["myId"] = ws.WorkShiftId;
     return RedirectToAction(nameof(CreateSharedView),new { workShiftId= ws.WorkShiftId});
    

    CreateSharedView GET Action:

    public IActionResult CreateSharedView([FromQuery]int? workShiftId)
    {
        //Or  ViewData["workShiftId"] = TempData["myId"]
        ViewData["workShiftId"] = workShiftId;
        return View();
    }
    

    CreateSharedView View:

    <form asp-action="CreateSharedView">
    
        <input type="hidden" name="workShiftId" value="@ViewBag.workShiftId" />
    </form>
    

    CreateSharedView POST Action

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CreateSharedView(    int workShiftId, 
                                                          SharedViewModel sharedViewModel, 
                                                          SurveyViewModel surveyViewModel,
                                                          WorkShiftTask task)
    {
          task.WorkShiftTaskId = workShiftId;
    }