0

I've looked at many other similar questions to mine, but I haven't found one where the goal was quite the same. I'm getting the rows for my datatable from a database using an ADO.NET Entity Model in ASP.NET MVC4. I want to be able to click a link on any given row, and have that link take me to a completely different datatable of rows that are from a completely different database table but are associated with the link from the row that I clicked on. The data that binds the clicked link to the new datatable I want to see is in a hidden column in each row. I've figured out that I can get to that data from the "mRender" function via the indices that correspond to my view model in the full[] array.

I am very green at both MVC and jQuery, so please bear with me as you read my code.

Here is my view:

@using System.Web.Optimization

@{
    ViewBag.Title = "ShowPeople";
}

<h2>ShowPeople</h2>
<div id="example_wrapper" class="dataTables_wrapper form-inline" role="grid">
    <table border="0" class="table table-striped table-bordered dataTable" id="people" aria-describedby="example_info">
        <thead>
            <tr role="row">
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header1</th>
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header2</th>
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header3</th>
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header4</th>
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header5</th>
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header6</th>
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header7</th>
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header8</th>
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header9</th>
                <th class="sorting" role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">Header10</th>
                <th role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">ID_PART1</th>
                <th role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">ID_PART2</th>
                <th role="columnheader" tabindex="0" rowspan="1" colspan="1" aria-controls="example">LINKS</th>
            </tr>
        </thead>
        <tbody role="alert" aria-live="polite" aria-relevant="all"></tbody>
    </table>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $('#people').dataTable({
            "bServerSide": true,
            "sAjaxSource": "DataAjaxHandler",
            "bProcessing": true,
            "sPaginationType": "full_numbers",
            "aoColumns": [
                            { "sName": "data1" },
                            { "sName": "data2" },
                            { "sName": "data3" },
                            { "sName": "data4" },
                            { "sName": "data5" },
                            { "sName": "data6" },
                            { "sName": "data7" },
                            { "sName": "data8" },
                            { "sName": "data9" },
                            { "sName": "data10" },
                            {
                                "sName": "ID_ONE",
                                "bVisible": false,
                                "bSearchable": false,
                                "bSortable": false,
                                "mData": null
                            },
                            {
                                "sName": "ID_TWO",
                                "bVisible": false,
                                "bSearchable": false,
                                "bSortable": false,
                                "mData": null
                            },
                            {
                                "sName": "Links",
                                "bSortable": false,
                                "bSearchable": false,
                                "mData": null,
                                "mRender": function (data, type, full) {
                                    var urlBase = "@Url.Action("ShowData", "Data")";

                                    return "<a href='" + urlBase + "/?id1=" + full[10] + "&id2=" + full[11] + "'>Events</a>";
                                }
                            }
            ]
        });
    });
</script>

Here is my controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web_UI.Models;

namespace Web_UI.Controllers
{
    public class DataController : Controller
    {
        //
        // GET: /Reports/

        public ActionResult ShowData()
        {
            return View();
        }

        public ActionResult DataAjaxHandler(jQueryDataTableParamModel param)
        {
            //How can I get those IDs?
        }
    }
}

What I'm trying to figure out is if there is a way to get the data from my hidden ID columns to my controller's ajaxhandler action method. I know how to get the data I need from my database using linq, but I need the data from those ID columns for the specific row whose link was clicked to get it. Is there any way I can accomplish this?

Many of the questions I've already read through involve pushing data with a "fnServerParams" function, but I don't really understand how it works and all the examples I've seen look very static (as in they always push the same data). The code that provides the URL for the links right now doesn't currently do anything as it was simply an exercise in figuring out how to access the data from some hidden columns. Edit: Although I said the link URLs don't go anywhere, I didn't mean to imply that full[10] and full[11] aren't getting the right values. I've checked the URLs that my code generates and they do indeed get the correct ID numbers for each row. I just don't know how to pass them back to the server for use in my controller.

Thanks in advance for any help.

4

1 回答 1

0

要将数据发送回控制器,您应该使用 ajax 调用。

$.ajax({
     url: "@(Url.Action("Action", "Controller"))",
     type: "POST",
     cache: false,
     async: true,
     data: { id: 'id' },
     success: function (result) {
         (do something)
     }
});

您可以通过数据传递多个字段作为数据:'data,data1:'data1'等

于 2013-09-29T00:20:33.760 回答