简体   繁体   English

Ajax.BeginForm不发送和AjaxRequest

[英]Ajax.BeginForm do not send and AjaxRequest

I want to use the Ajax.BeginForm to update a table without reload the full page, when i submit the form the request is not an ajax one , or in the controller method when i ask if the request is ajax it never enter inside the if sentence. 我想使用Ajax.BeginForm在不重新加载整个页面的情况下更新表,当我提交表单时,请求不是ajax,或者在控制器方法中,当我问请求是否为ajax时,它永远不会输入if句子。 So i have this in my files: 所以我在我的文件中有这个:

On the Partial View 在局部视图上

@model IEnumerable<Fighting.DTO.N_pais.OutN_pais>

<table class="table table-advance" id="table_paises">
    <thead>
        <tr>
            <th style="width: 18px">
                <input type="checkbox" /></th>
            <th>Nombre</th>
            <th>Editar</th>
            <th>Eliminar</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr class="table-flag-blue">
                <td>
                    <input type="checkbox" value="@item.ID_PAIS" /></td>
                <td>@item.NOMBRE</td>
                <td><a class="btn btn-circle" data-toggle="modal" href="#modal-2" onclick="Edit(@item.ID_PAIS)"><i class="fa fa-edit"></i></a></td>
                <td><a class="btn btn-circle" data-toggle="modal" href="#modal-3" onclick="Delete(@item.ID_PAIS, @item.NOMBRE)"><i class="fa fa-trash-o"></i></a></td>
            </tr>
        }
    </tbody>
</table>

In the view 在视图中

<div class="row">
    <div class="col-md-12">
        <div class="box">
            <div class="box-content">
                <div class="btn-toolbar pull-right clearfix">
                    <div class="btn-group">
                        <a class="btn btn-circle show-tooltip" title="Adicionar Pa&iacute;s" href="#modal-1" data-toggle="modal"><i class="fa fa-plus"></i></a>
                        <a class="btn btn-circle show-tooltip" title="Edit selected" href="#modal-2" data-toggle="modal"><i class="fa fa-edit"></i></a>
                        <a class="btn btn-circle show-tooltip" title="Delete selected" href="#modal-3" data-toggle="modal"><i class="fa fa-trash-o"></i></a>
                    </div>
                    <div id="form-reload" class="btn-group">
                        @using (Ajax.BeginForm("Obtener", "NPais", new AjaxOptions
                        {
                            HttpMethod = "get",
                            InsertionMode = InsertionMode.Replace,
                            UpdateTargetId = "table_paises"
                        }))
                        {
                            <button class="btn btn-circle show-tooltip" title="Recargar" type="submit"><i class="fa fa-repeat"></i></button>                            
                        }
                    </div>
                </div>
                <br />
                <br />
                <div class="clearfix"></div>
                <div class="table-responsive" style="border: 0">
                    @Html.Partial("_TablePais", Model)
                </div>
            </div>
        </div>
    </div>
</div>

On the Controller 在控制器上

public object Obtener()
    {
        var pais = _n_pais.GetAll().ProjectedAsCollection<OutN_pais>().ToList();

        var paises = from r in pais select r;

        return paises;
    }

    public override ActionResult Obtener(InUi input)
    {
        if (Request.IsAjaxRequest())
        {
            return PartialView("_TablePais", Obtener());
        }

        return View(Obtener());
    }

The javascripts JavaScripts

@Scripts.Render("~/Content/Flaty/assets/jquery/jquery-2.0.3.min.js")
@Scripts.Render("~/Content/Flaty/assets/bootstrap/js/bootstrap.min.js")
@Scripts.Render("~/Content/Flaty/assets/jquery-slimscroll/jquery.slimscroll.min.js")
@Scripts.Render("~/Content/Flaty/assets/jquery-cookie/jquery.cookie.js")
@Scripts.Render("~/Content/Flaty/assets/jquery-unobtrusive-ajax/jquery.unobtrusive-ajax.js")

What am I missing? 我想念什么?

You'll get this behavior if the unobtrusive ajax script runs into an error while executing and bails, leaving the browser to handle the submit itself. 如果不引人注目的ajax脚本在执行和保释时遇到错误,而让浏览器自行处理提交,则会出现此行为。 In my experience, this most often happens when there is a name mismatch. 以我的经验,这最经常发生在名称不匹配的情况下。 Perhaps your partial view is not returning an object whose ID is "table_paises", and the script is therefore unable to find a target object? 也许您的局部视图没有返回ID为“ table_paises”的对象,因此脚本无法找到目标对象?

Either way, debugging your code in the browser (F12) should surface your error. 无论哪种方式,在浏览器(F12)中调试代码都应显示错误。

In my case I was forgetting to load the jquery.unobtrusive-ajax.min.js file. 就我而言,我忘记加载jquery.unobtrusive-ajax.min.js文件。

I solved it by adding the following line of code 我通过添加以下代码行解决了它

@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")

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

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