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

4

我有一个带有单个输入和按钮的Ajax表单。提交时,表单应该只将输入的值发布到一个操作方法。我的表单正确地发布到了User控制器的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
    }
}

感谢您,Keith。

你好 Keith,你找到这个问题的答案了吗? - Rahul
1个回答

2

对于这个问题,我发现将一个新的{target="_self"}作为htmlAttributes对象添加即可,示例如下:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接