0

I'm not confident that the title is well put so feel free to criticize. :)

I have a controller that returns a page on which the user can click on some options creating a list of items (dynamically built up on the client using JS). As the user is satisfied, they can click on a button and then...

...currently, some DIVs are hidden/displayed, converting the (same) page to a read-only viewer of the selection.

...optimally, another ActionResult should be invoked presenting the information.

My biff is that I can't decide on a good way to transfer the data from one page to another: query string is one option, storing/retrieving to/from DB is an other. I'm not happy with any of these.

What would be a smooth and recommended way to transfer the data to a new view withing the same controller?

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • I prefer an ajax call for doing that – Matt Bodily Nov 13 '13 at 14:58
  • @MattBodily Agreed. But what'd be a recommended way to transport the collected data? – Konrad Viltersten Nov 13 '13 at 15:02
  • I would recommend an Ajax call that saves the data in a global variable from the same controller (since you don't want to store and retrieve from db). – Guillermo Oramas R. Nov 13 '13 at 15:03
  • @Guillelon Please elaborate, as I'm apparently missing something here. Say that I've got an IMG that upon a click makes a *$.ajax* magic. But how is that supposed to access the server-side stuff? I'm missing something obvious, don't I? – Konrad Viltersten Nov 13 '13 at 15:10
  • I send the data through ajax. If it is a large amount then I create a model for it and then create a js object that matches the model. A bit of work to set up but is pretty clean to send and receive on the server side – Matt Bodily Nov 13 '13 at 15:20
  • @MattBodily I won't have much data so creating a model is an overkill. However, could you please elaborate on the "sending through ajax"-part? I can make an AJAX call with custom fields called *beep* and *boop* but I'm not clear on how to read these on the server-side. Suggestions or hints? – Konrad Viltersten Nov 13 '13 at 15:25
  • @KonradViltersten, what I'm saying is close to Matt's idea. You send the data trough ajax to the server, the server receive this data and storage it in a global variable in the controller, this global variable can be a object used as model or just a primitive data variable, then when you call the other method in the controller, this new method is going to check the global variable to see the data stored and send it to the new view. Let me know ;) – Guillermo Oramas R. Nov 13 '13 at 15:26
  • @Guillelon I believe I'm not asking the right question, here. HOW exactly and technically do we send the data to the server? Query string? Too lengthy... Parameters divided by slashes? Painful to translate. :) – Konrad Viltersten Nov 13 '13 at 15:38
  • @KonradViltersten Matt answer it for me. It's an Ajax call that sends the data like a Json file. The answer by Matt explains how to send the data, once you have it en the server you can follow my other recommendations. – Guillermo Oramas R. Nov 13 '13 at 15:43

1 Answers1

2
$.ajax({
    url: '@(Url.Action("Action", "Controller"))',
    type: 'post',
    data: {
        id: id,
        data1: data1
    },
    success: function (result) {
        if (result.Success) {
        }
});

A very simple way is like the above where you define as many fields as you want and as long as the input parameters match they will be received on the controller

public ActionResult Action(string id, string data1){...

if you want to get more complicated you can build lists and arrays with the json data and then it is usually a good idea to stringify it.

var data = {};
data.id = 'id';
data.list = [];
data.list.push({ name: 'name', location: 'location', etc })

then in the ajax call

data: Json.stringify(data),

again, as long as the names match the controller will receive it. Hope this helps

Edit: Json.stringify is a tool that is used for sending the data. I don't know all of the details of what it does but it is recommended to use for more complex data. The example here I used for sending a model back to the controller but you mentioned not wanting to create a model. I believe to receive this data on the controller side you need to have input parameters that match what is defined in data. From what I have above list is a complex type so your controller would be something like this.

Public ActionResult Action(string id, List<ComplexType> list){...
Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
  • Got it. Now a follow-up question. I'm assuming that the action result returned gets into *data* in *success* function. What if I'm returning not *View()* but a *File(arr, "application/pdf", "FileName.pdf")*? How do I "push" the PDF to the user for download? Or is it perhaps unrelated and I should ask another question? – Konrad Viltersten Nov 13 '13 at 15:54
  • I haven't provided a pdf to users before so not really sure. Just googled it and found this link http://stackoverflow.com/questions/730699/how-do-i-code-for-pdf-downloads. Hopefully it helps – Matt Bodily Nov 13 '13 at 16:14
  • If I call my parts of *data* like *id* and *data1*, the parameters on the server side need to be named *id* and *data1* as well, right? But what do I call the parameter that is passed by *data: Json.stringify(...)*? – Konrad Viltersten Nov 13 '13 at 16:19
  • Would you take a look at [my follow-up question](http://stackoverflow.com/questions/19980232/designing-data-in-ajax-to-match-server-side-model-definition) and give me a hint on the management of abstract data to for matching between AJAX calls and data model server-side? – Konrad Viltersten Nov 15 '13 at 13:25
  • sorry for the confusion with this answer. From your other question looks like my example here was a lot more complicated than what you were needing – Matt Bodily Nov 15 '13 at 15:07