如何从一个方法中的对象中检索值以在另一个方法中使用它?

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

我知道这个问题已经问了很多次,但它似乎对我不起作用。你能帮忙或给我一个类似问题的 link 吗?

我有一个对象“ws”,我想捕获 WorkShiftID,以便我可以在 CreateSharedView 中使用它。

Ps:对不起我的英语,我是 c# 的新手所以请多多包涵。谢谢

编辑:我想从 Create 中检索 ws.WorkShiftId 并将其放入 CreateSharedView 的 task.WorkShiftId 中。可能吗?

        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);
        }```


CreateSharedView POST 方法基本上只是向数据库添加一个 WorkShiftTask。因此,与其尝试从另一个方法调用一个 POST 方法,不如从 Create() 方法内部执行此操作:

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; test? no. defined where?
        _context.Add(emp);

        WorkShiftTask task = new WorkShiftTask();
        task.WorkShiftId = ws.WorkShiftId;  // There's the ws.WorkShiftId you were mentioning...
        // set other members of "task"
        _context.Add(task);

        await _context.SaveChangesAsync();
    }
}

最简单的方法是使用Session来存储和获取ws.WorkShiftId,在你启动configuring session state之后,

CreatePOST操作中:

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

CreateSharedView POST 操作中:

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

另一种更复杂的方法是,当您重定向或使用 TempData:

时,您可以将 ws.WorkShiftId 作为参数传递

Create POST 操作:

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

CreateSharedView 获取操作:

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

CreateSharedView 查看:

<form asp-action="CreateSharedView">

    <input type="hidden" name="workShiftId" value="@ViewBag.workShiftId" />
</form>

CreateSharedViewPOST动作

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