Asp.net mvc 4 在ASP.NET MVC 4中创建具有友好URL的向导
我正在开发一个ASP.NETMVC4应用程序。此应用程序中需要一个向导。向导包含三个屏幕。我需要他们的URL映射到:Asp.net mvc 4 在ASP.NET MVC 4中创建具有友好URL的向导,asp.net-mvc-4,wizard,Asp.net Mvc 4,Wizard,我正在开发一个ASP.NETMVC4应用程序。此应用程序中需要一个向导。向导包含三个屏幕。我需要他们的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); }
/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.beginnform()
的每个向导视图中,确保调用了一个重载,该重载接受所需的路由或所需的控制器、操作和其他路由参数。例如,在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/action/id),每个步骤都有自己的URL