使用文本框加载Jquery Datatable的问题

问题描述 投票:0回答:1

希望有人可以帮助我。

这是我的控制器

 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中显示数据,在当前状态下我能够执行我的程序并获取我想要的所有数据,但是带来数据后,它不会返回带有表的视图,它只返回普通数据。

抱歉错误和我的英语不好。

请帮忙 :(

c# jquery json asp.net-mvc datatables
1个回答
0
投票

我使用web 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({
               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) }
           });
© www.soinside.com 2019 - 2024. All rights reserved.