c# - 使用 ASP.net MVC 执行提交(回发)和重定向

标签 c# asp.net-mvc

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

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

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

最佳答案

如果您使用 Html.BeginForm,就会出现该帖子像这样:

<% using(Html.BeginForm("HandleForm", "Home")) { %>
    <fieldset>
        <legend>Fields</legend>
        <p>
            <%= Html.TextBoxFor(m => m.Field1) %>
        </p>
        <p>
            <%= Html.TextBoxFor(m => m.Field2) %>
        </p>
        <p>
            <input type="submit" value="Submit" />
        </p> 
    </fieldset>
<% } %>

然后您的 Controller 操作可以执行重定向:

[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);    
}

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

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

关于c# - 使用 ASP.net MVC 执行提交(回发)和重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9978348/

相关文章:

c# - UnobservedTaskException 被抛出,但它由 TaskScheduler.UnobservedTaskException 处理程序和 continuations OnlyOnFaulted 处理程序处理

c# - Visual Studio 2010 中的 "Cannot evaluate expression because the code of the current method is optimized"

c# - ASP.NET - 如果角色授权失败则重定向到错误页面

c# - 为什么不调用 GetVaryByCustomString

c# - 如何防止 ASP.NET MVC 中的某些 View 被缓存?

asp.net-mvc - 用于导航到 T4MVC 链接的 Visual Studio 宏

C#: for 诗句 foreach

c# - PCL .NET 4.5 计时器

c# - 使用 linq 将 XML 元素连接到 C# 中的类属性

asp.net - 将模型传递给 MVCMailer View