Imagine a view where there are questions and answers - each answer has a radio button - when the questions are all answered, the user presses a button - a javascript event handler for the button press is executed - jquery is used to retrieve all the checked
radio buttons and their related values.
What is the proper method to "JSON" ify this data and then pass to a controller? Thanks in advance for any help.
The data returned by the jquery call is already a collection. Does a java script array need to be defined and then populated with the values from the jquery? Further, what is the singature for the passed param at the controller?
The original question was how do I make data that's in the browser into JSON so I can send to the server. I sort of assumed you knew how to make an Ajax call, so I left that part out and only mentioned how to take objects and convert into JSON (that presumably
would be sent on that Ajax call). Perhaps you weren't familiar with making the Ajax call?
vinceweeks
The data returned by the jquery call is already a collection. Does a java script array need to be defined and then populated with the values from the jquery? Further, what is the singature for the passed param at the controller?
So now I'm confused -- the data you're now talking about is being returned from an Ajax call and not collected from the client? This isn't making sense. Perhaps we can start over? :/
You can accept an object as a paramater to get object model binding -- there are examples of this on www.asp.net/mvc/tutorials.
Basically, the class type you accept as a paramater must have the same "shape" as the Json datra being submitted. The plumbing just uses duck typing, so if it fits, it ... binds (hmm, was hoping to make a pun, but no luck).
So, check out Json model binding --
here's a hit from a search that looks like it shows the basics.
Oh and yes, object model binding supports collections -- your param can be an array, List<T>, whatever -- if it fits... well, like I already said :)
Well, thanks for your help. What I was looking for was a complete answer. For the sake of other n00bs who might have a similar question, here IS the complete answer.
In the View:
<script type="text/javascript">
function DoneWithTest()
{
var selected = $("input:radio:checked");
var selArray = new Array();
for (var a=0; a< selected.length; a++)
{
selArray.push(selected[a].value);
}
$.ajax({
url: '../../Test/FinishedTest',
data: JSON.stringify(selArray),
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (result) {
},
error: function ()
{
alert("error");
}
});
}
</script>
and in the Controller:
public ActionResult FinishedTest(List<string> selArray)
{
//do something with data
return View("Index");
}
It's a little disconcerting that an action like this, which would seem to me, to be a very commonplace occurence, should have such a lack of instructions. I searched many sites looking for a simple answer to what I felt was a simple question, and really
never found what I was looking for. I urge all of my colleagues to, when encountering this situation, post clear concise examples for the benfit of others in the community. Again, thanks for your help.
Marked as answer by vinceweeks on Apr 27, 2012 02:14 AM
vinceweeks
0 Points
18 Posts
"post" ing json data to a controller method
Apr 26, 2012 07:45 PM|LINK
Imagine a view where there are questions and answers - each answer has a radio button - when the questions are all answered, the user presses a button - a javascript event handler for the button press is executed - jquery is used to retrieve all the checked radio buttons and their related values.
What is the proper method to "JSON" ify this data and then pass to a controller? Thanks in advance for any help.
BrockAllen
All-Star
27574 Points
4912 Posts
MVP
Re: "post" ing json data to a controller method
Apr 26, 2012 07:59 PM|LINK
You can just do this:
var data = { prop1: val1, prop2: val2 };
var json = JSON.stringify(data);
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
vinceweeks
0 Points
18 Posts
Re: "post" ing json data to a controller method
Apr 26, 2012 08:37 PM|LINK
The data returned by the jquery call is already a collection. Does a java script array need to be defined and then populated with the values from the jquery? Further, what is the singature for the passed param at the controller?
BrockAllen
All-Star
27574 Points
4912 Posts
MVP
Re: "post" ing json data to a controller method
Apr 26, 2012 08:51 PM|LINK
The original question was how do I make data that's in the browser into JSON so I can send to the server. I sort of assumed you knew how to make an Ajax call, so I left that part out and only mentioned how to take objects and convert into JSON (that presumably would be sent on that Ajax call). Perhaps you weren't familiar with making the Ajax call?
So now I'm confused -- the data you're now talking about is being returned from an Ajax call and not collected from the client? This isn't making sense. Perhaps we can start over? :/
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
vinceweeks
0 Points
18 Posts
Re: "post" ing json data to a controller method
Apr 26, 2012 09:56 PM|LINK
hmmm - let's take your original answer... How would you pass that to a controller?
public ActionResult ControllerMethod(?) would it be string? List<string>? a custom defined type?
To reiterate: How do you JSON ify a jquery result - you say stringify - OK
If I use stringify, what type does the controller want to see for it's input parameter?
BrockAllen
All-Star
27574 Points
4912 Posts
MVP
Re: "post" ing json data to a controller method
Apr 26, 2012 10:54 PM|LINK
You can accept an object as a paramater to get object model binding -- there are examples of this on www.asp.net/mvc/tutorials.
Basically, the class type you accept as a paramater must have the same "shape" as the Json datra being submitted. The plumbing just uses duck typing, so if it fits, it ... binds (hmm, was hoping to make a pun, but no luck).
So, check out Json model binding -- here's a hit from a search that looks like it shows the basics.
Oh and yes, object model binding supports collections -- your param can be an array, List<T>, whatever -- if it fits... well, like I already said :)
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
vinceweeks
0 Points
18 Posts
Re: "post" ing json data to a controller method
Apr 27, 2012 02:13 AM|LINK
Well, thanks for your help. What I was looking for was a complete answer. For the sake of other n00bs who might have a similar question, here IS the complete answer.
In the View:
<script type="text/javascript"> function DoneWithTest() { var selected = $("input:radio:checked"); var selArray = new Array(); for (var a=0; a< selected.length; a++) { selArray.push(selected[a].value); } $.ajax({ url: '../../Test/FinishedTest', data: JSON.stringify(selArray), type: 'POST', contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (result) { }, error: function () { alert("error"); } }); } </script>and in the Controller:
public ActionResult FinishedTest(List<string> selArray) { //do something with data return View("Index"); }It's a little disconcerting that an action like this, which would seem to me, to be a very commonplace occurence, should have such a lack of instructions. I searched many sites looking for a simple answer to what I felt was a simple question, and really never found what I was looking for. I urge all of my colleagues to, when encountering this situation, post clear concise examples for the benfit of others in the community. Again, thanks for your help.