redirectToAction 正在重置数组
Posted
技术标签:
【中文标题】redirectToAction 正在重置数组【英文标题】:redirectToAction is resetting array 【发布时间】:2021-04-07 23:47:51 【问题描述】:您好,我正在为这个 Web 应用程序使用 C#,并且我正在尝试通过 redirectToAction 传递对象 stdnt。但是当它刷新并进入索引时,skillResults 总是为空的。我单步执行到提交的末尾,它包含所有正确的信息,但是当我单步执行索引时它被重置。任何帮助将不胜感激。
public IActionResult Index(Student stdnt)
return View(stdnt);
/// <summary>
/// submit student skills
/// </summary>
/// <param name="con"></param>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult> submit(Student stdnt)
stdnt = IsValidStudent(stdnt);
if (stdnt.warning == "")
//initialize arrays
stdnt.skillResults = new SkillResult[9];
for(int i = 0; i < 9; i++)
stdnt.skillResults[i] = new SkillResult();
stdnt.skillResults[i].skill_name = "";
stdnt.skillResults[i].skill_type = "";
stdnt.skillResults[i].description = "";
stdnt.skillResults[i].importance = "";
stdnt.success = true;
//find skills for the three jobs listed
for (int i = 0; i < 3; i++)
if (!string.IsNullOrEmpty(stdnt.JobNames[i]))
var jobList = new List<Job>();
jobList = await getJobs(stdnt.JobNames[i]);
var jobSkills = new JobSkills();
jobSkills = await getJobSkills(jobList[i].uuid);
//find top three skills based on importance level
var importantSkills = new List<Skill>();
for (int j = 0; j < 3; j++)
importantSkills.Add(new Skill() level = 0 );
for (int j = 0; j < jobSkills.skills.Count; j++)
if (jobSkills.skills[j].level > importantSkills[0].level)
importantSkills[0] = jobSkills.skills[j];
else if (jobSkills.skills[j].level > importantSkills[1].level)
importantSkills[1] = jobSkills.skills[j];
else if (jobSkills.skills[j].level > importantSkills[2].level)
importantSkills[2] = jobSkills.skills[j];
//set students results
for(int j = 0;j < 3; j++)
stdnt.skillResults[j + i * 3].skill_name = importantSkills[j].skill_name;
stdnt.skillResults[j + i * 3].skill_type = importantSkills[j].skill_type;
stdnt.skillResults[j + i * 3].description = importantSkills[j].description;
stdnt.skillResults[j + i * 3].importance = importantSkills[j].importance.ToString();
else
stdnt.success = false;
return RedirectToAction("Index", stdnt);
【问题讨论】:
【参考方案1】:正如 Courtwright (https://***.com/a/129361/14879818) 所述帖子不支持重定向。
为了接收你的对象stdnt
,你需要使用装饰器[HttpGet]
。
【讨论】:
以上是关于redirectToAction 正在重置数组的主要内容,如果未能解决你的问题,请参考以下文章
从另一个网站导航时 RedirectToAction 不起作用
在没有 RedirectToAction() 的 SignOut() 之后清除 Request.IsAuthenticated 值
MVC RedirectToAction 在 global.asax 中调用 Session_Start()