gpt4 book ai didi

asp.net-mvc-4 - 在 ASP.NET MVC 4 中创建具有友好 URL 的向导

转载 作者:行者123 更新时间:2023-12-02 20:19:26 25 4
gpt4 key购买 nike

我正在开发 ASP.NET MVC 4 应用程序。这个应用程序需要一个向导。该向导包含三个屏幕。我需要将它们的 URL 映射到:

/wizard/step-1
/wizard/step-2
/wizard/step-3

在我的 WizardController 中,我执行以下操作:

public ActionResult Step1()
{
var model = new Step1Model();
return View("~/Views/Wizard/Step1.cshtml", model);
}

[HttpPost]
public ActionResult AddStep1(Step1Model previousModel)
{
var model = new Step2Model();
model.SomeValue = previousModel.SomeValue;

return View("~/Views/Wizard/Step2.cshtml", model);
}

[HttpPost]
public ActionResult AddStep2(Step2Model previousModel)
{
var model = new Step3Model();
return View("~/Views/Wizard/Step3.cshtml", model);
}

虽然这种方法有效,但我的问题是浏览器 URL 没有更新。如何发布步骤中的值并将用户重定向到具有不同数据模型的新 URL?

谢谢!

最佳答案

在调用 Html.BeginForm() 的每个向导 View 中,请确保调用它的重载,该重载接受所需的路由或所需的 Controller 、操作和其他路由参数。例如,在Step1.cshtml中:

@using (Html.BeginForm("Step-2", "MyWizard")) {
// put view stuff in here for step #1, which will post to step #2
}

这将使目标 URL“漂亮”,但不会修复操作名称本身“丑陋”的问题。为了解决这个问题,MVC 中有一个功能可以将操作方法​​“重命名”为几乎任何您想要的内容:

[HttpPost]
[ActionName("step-2")] // this will make the effective name of this action be "step-2" instead of "AddStep1"
public ActionResult AddStep1(Step1Model previousModel)
{
// code here
}

假设应用程序使用默认的 MVC 路由( Controller /操作/id),每个步骤都有自己的 URL。

关于asp.net-mvc-4 - 在 ASP.NET MVC 4 中创建具有友好 URL 的向导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14713060/

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