执行提交(回发)并使用ASP.net MVC重定向

我想使用从我的标记submit到ASP.net MVC操作。

然后我想将请求重定向到另一个URL。

我可以这样做吗? 或MVC只对应ajax?

如果您正在使用Html.BeginForm,则会发生这样的post:

 <% using(Html.BeginForm("HandleForm", "Home")) { %> 
Fields

<%= Html.TextBoxFor(m => m.Field1) %>

<%= Html.TextBoxFor(m => m.Field2) %>

<% } %>

然后您的控制器操作可以执行重定向:

 [AcceptVerbs(HttpVerbs.Post)] public ActionResult HandleForm(MyModel myModel) { // Do whatever you need to here. return RedirectToAction("OtherAction", myModel); } public ActionResult OtherAction(MyModel myModel) { return View(myModel); } 

编辑::上面的示例现在将绑定以下模型,并可以在操作之间传递:

 public class MyModel { public string Field1 { get; set; } public string Field1 { get; set; } } 

下面的代码演示了如何在用户提交表单后将用户重定向到另一个操作。

如果要保留要在重定向到的操作方法中使用的任何提交数据,则需要将其存储在TempData对象中。

 public class HomeController : Controller { [HttpGet] public ActionResult Index() { // Get the e-mail address previously submitted by the user if it // exists, or use an empty string if it doesn't return View(TempData["email"] ?? string.Empty); } [HttpPost] public ActionResult Index(string email) { // Store the e-mail address submitted by the form in TempData TempData["email"] = email; return RedirectToAction("Index"); } } 

您的Index视图如下所示:

 @using (Html.BeginForm("Index", "Home")) { @* Will populate the textbox with the previously submitted value, if any *@   }