简体   繁体   English

MVC中的Ajax刷新页面

[英]Ajax refreshing page in MVC

I'm using Ajax to pass some data to a Controller and save to database, and it works, the issue that is refreshing the page with every POST and I need to prevent that.我正在使用 Ajax 将一些数据传递给控制器​​并保存到数据库,它可以工作,问题是每次 POST 都刷新页面,我需要防止这种情况发生。

Ajax:阿贾克斯:

function AddComment(commet, auto) {
    $.ajax({
        type: "POST",
        url: '/bff/SaveComment',
        data: { id: idParte, commenta: commet, autoriza: auto },
        dataType: 'json',
        success: function (correct) {
            $("#win1").show().kendoWindow({
                width: "300px",
                height: "100px",
                modal: true,
                title: "Added"
            });
        },
        errror: function(inc) {
        }
    });
}

Controller:控制器:

[HttpPost]
public JsonResult SaveComment(int id, string commenta, string autoriza)
{
    // Some logic here
    return Json("");
}

Tried this way with correct.preventDefault();用正确的方式尝试过这种方式。preventDefault(); but didn't work但没有用

Is there a way to do it?有没有办法做到这一点?

EDITED:编辑:

This is my HTML:这是我的 HTML:

<form role="form">
<div class="form-group">
    @Html.Label("Commentario:")
    <textarea id="Comment" style="resize:none;" class="form-control"></textarea>
</div>
<div class="form-group">
    <input type="submit" value="Guardar" onclick="AddComment(Comment.value,'Comentario')" />
</div>
</form>

EDITED 2:编辑 2:

Fixed by changing type="submit" for type="button" Thanks to Hasta Pasta通过更改 type="submit" for type="button"感谢Hasta Pasta

i think you should put return false;我认为你应该把 return false; after the ajax request在ajax请求之后

function AddComment(commet, auto) {
    $.ajax({
        type: "POST",
        url: '/bff/SaveComment',
        data: { id: idParte, commenta: commet, autoriza: auto },
        dataType: 'json',
        success: function (correct) {
            $("#win1").show().kendoWindow({
                width: "300px",
                height: "100px",
                modal: true,
                title: "Added"
            });
        },
        error: function(inc) {
        }
    });
    return false;
}

based on you need to return false as you sayed :)基于你需要像你说的那样返回 false :)

<form role="form">
<div class="form-group">
    @Html.Label("Commentario:")
    <textarea id="Comment" style="resize:none;" class="form-control"></textarea>
</div>
<div class="form-group">
    <input type="submit" value="Guardar" onclick="return AddComment(Comment.value,'Comentario')" />
</div>
</form>

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

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