asp.net - 如何防止 Ajax.BeginForm 加载新页面?

标签 asp.net asp.net-mvc ajax.beginform

我有一个带有单个输入和一个按钮的 Ajax 表单。提交时,表单应仅将输入的值发布到操作方法。我的表单正确发布到用户 Controller 的 Log 方法,但完成后,页面重定向到/User/Log。

如何避免这种情况?


<% using (Ajax.BeginForm("Log", "User", null, new AjaxOptions {HttpMethod = "POST" }))
{%>
    Please enter the username: 
    <%= Html.TextBox("Username", "")%>
    <input type="submit" />
<% } %>

这里是操作方法:


public class UserController : Controller
{
    [AcceptVerbs(HttpVerbs.Post)]
    public void ForgotPassword(FormCollection collection)
    {
        string username = collection["Username"];

        //TODO:  some work
    }
}

谢谢, -基思

最佳答案

为此添加一个新的 { target = "_self"} 作为 htmlAttributes 对象对我有用

<% using (Ajax.BeginForm("Log", "User", null, new AjaxOptions {HttpMethod = "POST" }, new { target = "_self" }))
{%>

关于asp.net - 如何防止 Ajax.BeginForm 加载新页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2337602/

相关文章:

asp.net - 从 asp.net mvc 生成 PDF 文件

asp.net - AJAX Helper 和 async/await 之间的区别

asp.net - AJAX.BeginForm 发送空 FormCollection 列表到 MVC Controller

javascript - Ajax .BeginForm 在 IE9 中返回空白 View

c# - 如何通过显示文本在 ASP.NET 下拉列表中设置所选项目?

来自 Properties 的 C# 自定义属性

xml - 如何从 Web API 方法返回 Xml 数据?

asp.net-mvc - 在 MVC 中提交后保留密码文本框值

Jquery Ajax 仅在 Debug模式下工作

JQuery 和 Ajax.BeginForm() 两次发布相同的数据