使用文本框加载jQuery Datatable时出现问题

时间:2018-11-05 19:47:41

标签: c# jquery json asp.net-mvc datatables

希望有人可以帮助我解决这个问题。

这是我的控制器

 namespace PruebaBusquedaRun.Controllers
 {
public class TestController : Controller
{
    MandatosModel md = new MandatosModel();
    // GET: Test
    public ActionResult Index()
    {
        return View();
    }


    public ActionResult TestDataTable(string run)
    {


        List<MandatosModel> lmm = new List<MandatosModel>();

        DataSet ds = new DataSet();
        Int64 asd = Convert.ToInt64(run);
        Conexion.ConexionOra conexora = new Conexion.ConexionOra();

        ds = conexora.getMandatosPorRun(asd);

        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            lmm.Add(new MandatosModel
            {

                FOLIO = Convert.ToInt64(dr["FOLIO"]),
                IDCAJA = Convert.ToInt64(dr["IDCAJA"]),
                NOMBRES = dr["NOMBRES"].ToString(),
                A_PATERNO = dr["A_PATERNO"].ToString(),
                A_MATERNO = dr["A_MATERNO"].ToString(),
                CORREO = dr["CORREO"].ToString()

            });




        }


        return Json(new { data = lmm }, JsonRequestBehavior.AllowGet);
    }

}
}

这是我的观点

<div style="width:90%; margin:0 auto;">

@using (Html.BeginForm("TestDataTable", "Test", FormMethod.Post))
{
    <br />
    <input type="text" id="run" name="run" required />
    <button type="button" id="boton">Click Me!</button>
    <input type="submit" name="asd" value="Buscar Mandatos" />
    <br />
    <br />

}


<table id="myTable">
    <thead>
        <tr>
            <th>Folio</th>
            <th>Nombres</th>
            <th>Apellido Paterno</th>
            <th>Apellido Materno</th>
            <th>Correo</th>

        </tr>
    </thead>
</table>
</div>
<style>
tr.even {
    background-color: #F5F5F5 !important;
}
</style>
 @* Load datatable css *@
<!--<link href="//cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" 
rel="stylesheet" />-->
<link href="~/Content/DataTable/jquery.dataTables.css" rel="stylesheet" />
@* Load datatable js *@
@section Scripts{
<!--<script src="//cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"> 
</script>-->
<script src="~/Scripts/DataTable/jquery.dataTables.min.js"></script>
<script>
    $(document).ready(function () {
        $('#myTable').DataTable({
            "ajax": {
                "url": "/Test/TestDataTable",
                "type": "GET",
                "datatype": "json"


            },
            "columns": [
                { "data": "FOLIO", "autoWidth": true },
                { "data": "NOMBRES", "autoWidth": true },
                { "data": "A_PATERNO", "autoWidth": true },
                { "data": "A_MATERNO", "autoWidth": true },
                { "data": "CORREO", "autoWidth": true }

            ]
        });
    });

</script>
}

我要做的主要事情是将参数传递给TestDataTable方法(运行)并在DataTable中显示数据,在当前状态下,我可以执行我的过程并获取所有想要,但是带来数据后,它不返回带有表的视图,而是仅返回纯数据。

对不起,我的英语不好。

请帮助:(

1 个答案:

答案 0 :(得分:0)

我使用网络api,我这样发送数据

    ajax: {
        url: _json,
        type: "GET"
    },

所以您的_json中是url +参数

/Test/TestDataTable?run=demo

-

您需要我认为是,ajax调用您的控制器

    public JsonResult TestDataTable(string run)
    {
        try
        {
            //code
        }
        catch (Exception ex)
        {

            return Json(ex.Message);
        }
    }

像这样的一些ajax。

          $.ajax({
               cache: !1,
               url: '@Url.Action("TestDataTable", "TestController")',
                async: !1,
                data: { run: demo.val() },
               success: function (e) { // data for datatables },
               error: function (e, c, t) { alert(e.responseText) }
           });