【问题标题】:problem loading Jquery Datatable using a textbox使用文本框加载 Jquery 数据表的问题
【发布时间】:2019-04-09 05:08:22
【问题描述】:

希望有人可以帮助我。

这是我的控制器

 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】:

    我使用 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。

              $.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) }
               });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多