Question

I have a Controller that generates the following object and passes it to the View via the ViewBag:

public class ObjectList
{
  public ObjectItem[] Item { get; set; }
}


public class ObjectItem
{
  public string Name { get; set; }
  public decimal Value1 { get; set; }
  public decimal Value2 { get; set; }
}

In the view this data is displayed in a table. Blow the table I have an ActionLink that should pass all the data back to the Controller for further processing. How can I do this. Is a ActionLink the right choice, it can be hundreds of OjectItems in the List. Is there any other approach for this?

I hope you can help me with this.

Was it helpful?

Solution

I think you should store the data in the session. When the action link posts back, you would just grab the version of the data that is stored in the session.

As with the answer provided by @JTMon, if you use the action link to send the data back to the server you have to validate all the data (as it is now user input and could have been tampered with). You could use mechanisms such as a CRC or HMAC check.

OTHER TIPS

Not sure if there's a way to do it with MVC, but what i normally do is use jquery to go through the items in the table, push it to a javascript array, and then pass the array as parameter when making request to an MVC controller.

For example:

// Javascript variable that will contain the values
var selectedPositionNumbers = new Array();

// In my case, i store the value of the row when user click a checkbox in the same row
$('body').on('click', 'input[id*="IsSelected"]', function () {
            var positionNr = $(this).attr('id').split("IsSelected")[1];
            var isChecked = $(this).is(':checked');
            if (isChecked) {
                if ($.inArray(positionNr, selectedPositionNumbers) == -1) {
                    selectedPositionNumbers.push(positionNr);
                }
            }
        });

// And later on when making the request
$.ajax({
        url: '@Url.Action("SendSelectedValues")',
        data: JSON.stringify({ positionNrList: selectedPositionNumbers }),
        type: 'post',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            // Completed statement here
        }
    });

In my case i only need to store one value so i just have to make a string array, but for your case, maybe this article gives a better example: http://abiyh.blogspot.nl/2011/01/posting-javascript-array-using-jquery.html

If the information is of any critical value, I would definitely save a copy of it on the server (database, file system, etc.) and have the ActionLink submit some kind of id back to the server to help retrieve it. You would need to clean "unused" data (e.g. that was never exported and is no longer needed), but depending on your situation, it might be safer than risking user tempering with data that you think you have sent out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top