【发布时间】: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