【问题标题】:How do I retrieve a value from a object in an method to use it in another method?如何从方法中的对象中检索值以在另一个方法中使用它?
【发布时间】:2019-11-28 20:30:12
【问题描述】:

我知道这个问题已经问了很多时间,但它似乎对我不起作用。你能帮忙或提供一个类似问题的链接吗?

我有一个对象“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);
        }```


【问题讨论】:

  • 不清楚你的意思。当您说“在另一种方法中使用它”时,您指的是哪种方法?
  • 对不起,我想从 Create 中检索 ws.WorkShiftId 并将其放入 CreateSharedView 中的 task.WorkShiftId 中。有可能吗?
  • 您想从 Create 中调用 CreateSharedView() 的 GET 或 POST 版本吗?看起来您在命名 CreateSharedView 时已经在调用 RedirectToAction() ,但这将解析为 GET 方法。所以我假设你想调用 POST 方法?
  • @robbpriestley 是的,完全正确。抱歉,我的问题不清楚,感谢您抽出宝贵时间了解我要做什么

标签: c# asp.net-mvc asp.net-core model-view


【解决方案1】:

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

Create POST 操作中:

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

CreateSharedViewGET 操作:

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>

CreateSharedView POST 操作

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

【讨论】:

  • 您的解决方案非常完美!你得到了我想要做的事情。我已经坚持了一段时间,我不敢相信你的解决方案都有效!非常感谢您花时间回答我的问题。
【解决方案2】:

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

【讨论】:

  • 感谢您的回复!!可悲的是,这不起作用。那一刻,我去 CreateSharedView 我失去了 wsID。
  • 这就是我想说的:甚至不必费心去 CreateSharedView,只需从单个 POST 控制器 (Create) 完成所有操作。
猜你喜欢
  • 2018-10-19
  • 2011-11-01
  • 2015-10-31
  • 2017-04-23
  • 2022-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
相关资源
最近更新 更多