简体   繁体   English

防止表单提交后页面重定向/停留在同一页面上

[英]Prevent page redirect/stay on the same page upon form submit

Can't figure out for the life of me why this isn't working the way it should... 无法弄清楚我的一生为什么这没有按照应有的方式进行...

I've got a ASP.NET MVC5 form where there are two buttons--"Add New Product", will allow them to temporarily save all of the product data (NOT to the database) and allow them to input another information on a product; 我有一个ASP.NET MVC5表单,其中有两个按钮-“添加新产品”,将允许他们临时保存所有产品数据(不保存到数据库),并允许他们在产品上输入其他信息; the other button, "Save Changes", will save all of those products that were added via "Add New Product". 另一个按钮“保存更改”将保存所有通过“添加新产品”添加的产品。

I'm currently stuck on the "Add New Product" functionality. 我目前停留在“添加新产品”功能上。 Currently, it's redirecting users to a new page and I can't figure out why it's still redirecting when it technically shouldn't be. 目前,它会将用户重定向到新页面,但我不知道为什么在技术上不应该重定向时仍在重定向。 Any reason why it's doing a redirect? 进行重定向的任何原因? It's going through the controller method just fine too. 它也正在通过控制器方法。 I just want it to stay on the same page, but currently with what I have it's getting redirected to a blank page, the exact partial view I was on. 我只希望它保留在同一页面上,但是目前,随着我所拥有的内容,它正被重定向到空白页面,即我所处的确切局部视图。

Javascript Java脚本

$("#ProductForm").submit(function () {
    // Data validation here
    if (hasErrors) {
        // Display errors to user via unobtrusive validation
    }
    else {
        var ProductViewModel = {
            'Name': $("#ItemName").val(),
            'Price': $("#ItemPrice").val(),
            // etc.
        };

        $.ajax({
            url: $("#AddProductURL").val(),
            dataType: "JSON",
            type: "POST",
            data: ProductViewModel,
            success: function () {
                // Clear all fields
                $("#ProductForm").reset();
                return false; // I guess this isn't working?
            }
        });
        return false; // I guess this isn't working either?
    }
    return false; // Not working as well?
}

Controller 控制者

[HttpPost]
public ActionResult AddProduct(ProductViewModel productVM)
{
    // Do stuff with the view model here
    return new EmptyResult();
}

Try 尝试

$("#ProductForm").submit(function (e) {
e.preventDefault();
// Data validation here
if (hasErrors) {
    // Display errors to user via unobtrusive validation
}
... rest of the code

I figured it out. 我想到了。 I feel SUPER dumb. 我觉得超级傻。 I thought I mentioned this in the original post, but I'm using ASP.NET MVC5 and I didn't realize how relevant it was. 我以为我在原始帖子中提到了这一点,但是我使用的是ASP.NET MVC5,但我没有意识到它的重要性。 I'll edit my post in a moment to reflect it. 我将稍后编辑我的帖子以反映出来。

There's a difference between @Html.BeginForm and @Ajax.BeginForm which I found out through this Stack Overflow answer: difference between Html.BeginForm() and ajax.beginform() 我通过此堆栈溢出答案发现了@ Html.BeginForm和@ Ajax.BeginForm之间的区别: Html.BeginForm()和ajax.beginform()之间的区别

The very top bullet point for @Ajax.BeginForm answered my problem. @ Ajax.BeginForm的最重要的要点回答了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM