1

I'm using MVC3 to create a web application. In my web application I have a datatable displaying object values with a checkbox on each row that the user can check/uncheck. What I would like to do is to fix a setup so that the user can check/uncheck as many boxes as he/she wants without anything happening. Then when de user is done he/she presses a "done" button next to the table. Now this is when I want the magic to happen. I would like the view to collect all rows into an array so I can send the whole table to a controller method. I found in the datatables api that i can use the code

var table = $.fn.dataTable.fnTables(true);
if (table.length > 0) {
    $(table).dataTable().fnAdjustColumnSizing();
}

in order to save all the datatable data into a variable. Now how do I use this?

In addition to that I want all of this to happen when the user presses my Ajax.ActionLink that is currently today only returning another view. Would it be possible to also have my ActionLink set a variable to the return value of the javascript function that returns the array of the datatable rows?

I will try to display what I'm trying to say if I'm a bit unclear (I'm currently not the best web developer so please be gentle with me :)) Please see comment in code.

    @Ajax.ActionLink("Done",
                    "_DoneView",
                    new { value1 = Model.Item1.value1, value2 =
                    Model.Item1.value2, value3 = Model.Item1.value3,

                    //Would it be possible to here say something like
                    DatatableArray = javascriptFunction() //Javascript function
                    //that returns an array containing all rows from the table },

                    new AjaxOptions { HttpMethod = "GET",
                                      UpdateTargetId = "DataTable",
                                      InsertionMode = InsertionMode.Replace},
                    new {
                    @class = "linkButton blue"

Thanks for all the help and don't hesitate to ask for more info if there is something missing/is unlear.

1

1 Answer 1

2

This will be your new link:

<div id="divId">create a nice button with the div</div>

Make a controller that returns a JsonResult. Something like this:

public JsonResult GetAllReservations()
{
    var jsonlist = listOfYourReservations;  
    return Json(jsonlist, JsonRequestBehavior.AllowGet);
}

After this you can do like this in your jQuery:

$("#divId").on("click", function(){
    $.post('/ControllerName/GetAllReservations', function (data) {
        $.each(data, function(){

        });
    });
}
Sign up to request clarification or add additional context in comments.

7 Comments

I used reservations here as example. I don't know what your model is.
Hi and thank you for your help. I'm afraid I dont quite understand. Like I said I'm not very experienced with this. What I don't understand is where in my Ajax.ActionLink you want me to call GetAllReservations. If you suggest that I should replace "_DoneView" with GetAllReservations then how do I return my view? Btw in GetAllReservations is jsonList same as jsonlist?
I editted my answer, you can call your controller via the on click method of jQuery. What do you mean with this : Btw in GetAllReservations is jsonList same as jsonlist?
First time you write "jsonList" you use a capital L while the second time you write it you write with a small l. Anyway, I've updated my question. Alright. So I create a div like you suggest. I put by button "Done" in there. When I press "Done" the jquery on click function should then execute the code I from the datatables api? Have I understood this correct? Btw thank you for helping me out here, specially since I'm obviously not good at this :p
If my answer is correct please accept it and vote me up! :) It is a pleasure to help you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.