gpt4 book ai didi

asp.net-mvc - 如何在不传递参数的情况下重定向到带有其他 Action 参数的 Action ?

转载 作者:行者123 更新时间:2023-12-04 00:06:13 24 4
gpt4 key购买 nike

下面,在 CreateTest 中,一旦成功,我想从 CreateTest 重定向到测试。

我想做如下事情:

    public ActionResult Tests(int ID, string projectName)
{
TestModel model = new TestModel (ID, projectName);
return View(model);
}

[HttpPost]
public ActionResult CreateTest(TestModel model)
{
try
{
return RedirectToAction("Tests");
}
catch (Exception e)
{
ModelState.AddModelError("Error", e.Message);
return View(model);
}
}

最佳答案

您可能需要在重定向时提供参数:

return RedirectToAction("Tests", new { 
ID = model.ID,
projectName = model.ProjectName
});

你将重定向到的 url 现在看起来像这样:
/Foo/Tests?ID=123&projectName=abc

关于asp.net-mvc - 如何在不传递参数的情况下重定向到带有其他 Action 参数的 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5101919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com