【问题标题】:jQuery AJAX, conditional not workingjQuery AJAX,有条件的不起作用
【发布时间】:2014-11-05 20:38:53
【问题描述】:

我的函数通过 ID 和名称进行搜索,我在 MVC 中进行搜索。问题是它进入了条件 (if) 并且 AJAX 运行正确,但随后它也进入了 else 并运行它。显然我在对话框中的数据是空白的,因为它发送alert 窗口。它有时会正确打开对话框,但是当我更改模块并返回时,ifelse 会再次运行并出现空白对话框。

会发生什么?当我第一次点击时,它会阻塞,我再次点击然后数据出现......

function buscaProducto(url, cod, name) {

    if (cod.length != 0 || name.length != 0) {
        var producto = name;
        var identidad = cod;

        $.ajax({
            url: url,
            type: "POST",
            dataType: "html",
            error: AjaxFailure,
            beforeSend: AjaxBegin,
            data: { productoNombre: producto, identidad: identidad },

            success: function (data) {
                $("#dialog").dialog({
                    bigframe: true,
                    modal: true,
                    autoOpen: true,
                    width: 900,
                    heigth: 700,
                    resizable: false,
                });

                $("#progressbar").hide();
                $("#dialog").html(data);

                console.log("Entregó los datos al #dialog");

            }
        });
    }
    else {
        alert("<p>Debe ingresar una opcion de busqueda</p>", $(window).height() / 3)
        this.abort();
    }
} 

我认为缓存可能卡住了

控制器

[HttpPost]
        public ActionResult BusquedaProducto(string productoNombre, string identidad)
        {
            if (productoNombre.Equals(""))
            {
                if (identidad.Equals(""))
                {
                    return HttpNotFound();
                }
                else
                {
                    var code = (from p in db.GN_Portafolio
                                where p.CodigoPortafolio.StartsWith(identidad) && p.SenSerial == true
                                select p).ToList();
                    if (code.Equals("0"))
                    {
                        return HttpNotFound();
                    }
                    else
                    {
                        return View(code);
                    }
                }
            }
            else
            {
                var producto = (from p in db.GN_Portafolio
                                where p.NombrePortafolio.StartsWith(productoNombre)
                                select p).ToList().Take(100);
                if (producto.Equals("0"))
                {
                    return HttpNotFound();
                }
                else
                {
                    return View(producto);
                }
            }

        }

视图

@model IEnumerable<SifActivoFijo.Models.GN_Portafolio>

<form class="items">
    <label>items por Pagina: </label>
    <select>
        <option>5</option>
        <option>10</option>
        <option>15</option>
    </select>
</form>
<input name="button" type="button" onclick="$('#dialog').dialog('close');" value="Cerrar" />
<table  class="tablas">
    <thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CodigoPortafolio)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NombrePortafolio)
        </th>
        <th></th>
    </tr>
</thead>
    <tbody id="pagina">
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.CodigoPortafolio)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.NombrePortafolio)
                </td>
                <td>
                    <input class="seleccion" type="button" value="Seleccionar" />


                </td>
            </tr>
        }
    </tbody>
</table>
<div class="holder"></div>
<script type="text/javascript">
    $(document).ready(function () {

        $('input.seleccion').click(function () {
            var codigo = $(this).parent().prev().prev();
            var nombre = $(this).parent().prev();
            $('#activoFijo_GN_Portafolio_CodigoPortafolio').val($.trim(codigo.text()));
            $('#GN_Portafolio_CodigoPortafolio').val($.trim(codigo.text()));
            $('#nombrePortafolio').val($.trim(nombre.text()));
            $("#activoFijo_DescripcionActivoFijo").val($.trim(nombre.text()));
            document.getElementById("dialog").innerHTML = '<div id="progressbar" class="progressbar" style="display: none;"></div>';
            $("#dialog").dialog('close');
        });
    });

【问题讨论】:

  • 我不明白你的问题。也许你可以用西班牙语写,我会帮你翻译和回答。
  • 据我所见,代码是正确的。控制器是否返回正确的数据?错误可能在那里。
  • arriba publique controlador y vista.. gracias Arturo Torres Sánchez

标签: c# jquery asp.net-mvc model-view-controller


【解决方案1】:

您在服务器端方法中使用返回类型作为 ActionResult。所以这会返回您的整个视图,然后页面将重新加载。所以这发生了。因此,请将您的方法返回类型更改为任何其他类型,如字符串、Jsonresult 等,  

【讨论】:

  • 函数必须改变一些东西??使用 json 获得更好的数据?
猜你喜欢
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 2014-11-29
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
相关资源
最近更新 更多