需要一些帮助来尝试重定向到 View 。
我有一个名为“作业/索引”的 View /操作 简而言之,此 View 获取员工列表,并且我为每个员工提供了一个可供单击的按钮,以便他们可以查看分配给他们的工作。每个按钮执行Jobs/EmployeeJobs/Id,ID取决于其员工ID。
我有一个 View /操作,即 jobs/EmployeeJobs/2。 此 View 显示分配给 id 为 2 的员工的所有工作的列表,但显然会根据员工的不同而变化。
因此,在该页面上有“编辑”、“详细信息”和“删除”按钮。他们都工作得很好。 但是,当我尝试编辑一些工作信息并单击“保存”时。我的 URL 将我引导至 https://localhost:44327/Jobs。
当用户点击“保存”时,我想重定向回该工作人员的 View ,例如 https://localhost:44327/Jobs/EmployeeJobs/2。
这是我的作业 Controller 中的 EmployeeJobs/2 操作
`public async Task<IActionResult> EmployeeJobs(int? id)
{
var jobs = await _context.Jobs
.Include(j => j.Clients)
.Include(j => j.Employees)
.Include(j => j.Stage1)
.Include(j => j.Stage2)
.Include(j => j.Substages)
.Include(j => j.Substages2).Where(m => m.Employees.Id == id)
.ToListAsync();
return View(jobs);
}`
这是我的作业 Controller 中的 Edit/5 操作或 (POST)
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,JobNumber,Address,ClientID,StaffId,Notes,Stage1Id,Stage2Id,SubstagesId,Substages2Id,ConceptRequired,ConceptScheduleDate,ConceptSent,RevisionReceived,RevisionCompleted,ConceptApproved,MarketingRequired,MarketingScheduleDate,MarketingSent,MarketingCompleted,RenderRequired,RendererId,RenderSent,RenderReceived,ResourceRequired,PlannerId,ResourceScheduleDate,ResourceSent,ResourceCommentsReceived,ResourceCommentsCompeted,ResourceApproved,WorkingScheduleDate,WorkingCompleted,ReviewCompleted,TrussRequired,TrussId,TrussSent,TrussReceived,EngineerRequired,EngineerId,EngineerSent,EngineerReceived,ClientApprovalSent,ClientApprovalReceived,CouncilSubmission,CouncilId,BCNumber,RFIReceived,RFICompleted,BCGranted,AmendmentRequired,AmendmentScheduleDate,AmendmentCompleted,JobCompleted")] Jobs jobs)
{
if (id != jobs.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(jobs);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!JobsExists(jobs.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction("EmployeeJobs", new { id = id });
//return RedirectToAction("EmployeeJobs", new RouteValueDictionary(
// new { controller = "Jobs", action = "EmployeeJobs", id = id }));
}
return View(jobs);
}
正如你所看到的,我多次尝试了两种方法或RedirectToAction。奇怪的是,如果我的代码是,当我重定向时,我可以获得我想要的显示分配给该员工的工作的结果
return RedirectToAction("EmployeeJobs", new { id = 2 });
此代码将带我回到 ID 为 2 的员工。 所以希望有人能帮助我,请放轻松,我对编码还很陌生
这是我的工作模型中的相关信息
public class Jobs
{
public int Id { get; set; }
[Display(Name = "Job Number")]
[Required]
public int JobNumber { get; set; }
public string Address { get; set; }
// FK
[Display(Name = "Client")]
[ForeignKey("Clients")]
public int ClientID { get; set; }
// FK
[Display(Name = "Employee")]
[ForeignKey("Employees")]
public int StaffId { get; set; }
public string Notes { get; set; }
public virtual Clients Clients { get; set; }
public virtual Employees Employees { get; set; }
}
这是我的员工模型中的代码
public class Employees
{
public int Id { get; set; }
[Display(Name = "First Name")]
[RegularExpression(@"^[A-Z]+[a-zA-Z\s]*$")]
[StringLength(20, MinimumLength = 3)]
[Required]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[RegularExpression(@"^[A-Z]+[a-zA-Z\s]*$")]
[StringLength(20, MinimumLength = 3)]
[Required]
public string LastName { get; set; }
[Display(Name = "Contact Number")]
[StringLength(15, MinimumLength = 8)]
[Required]
public string ContactNumber { get; set; }
[StringLength(80, MinimumLength = 3)]
[Required]
public string Email { get; set; }
}
最佳答案
我认为当您查询数据库时,索引操作中存在错误。
这就是它无法返回新 View 的原因。您将 employeeId
与 jobId
混合在一起。对于 id=2 你有一个 Id =2 的员工,那么为什么它只适用于 id=2;
您的索引操作有一个非常困惑的查询,而且它不是异步的。
试试这个
var jobs = await _context.Jobs
.Include(j => j.Clients)
.Include(j => j.Employees)
.Include(j => j.Stage1)
.Include(j => j.Stage2)
.Include(j => j.Substages)
.Include(j => j.Substages2)
.Where(m => m.Employees.Id == id)
.ToListAsync();
return View(jobs);
但我想知道如何编译它
.Include(j => j.Employees)
.....
.Where(m => m.Employees.Id == id)
员工是一个人吗?
更新
看到你的工作类后,我认为你的位置没问题,但你也可以使用这个
.Where(m => m.StaffId == id)
并修复您的编辑操作
return RedirectToAction("EmployeeJobs", new { id = job.StaffId });
现在应该可以工作了
关于c# - 使用 ID 参数重定向到操作 - .Net Core Web App MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69683190/