allencoded
allencoded

Reputation: 7265

Using a javascript object in C#

I have a javascript object and would like to use it in my C# function but I am unsure how I can use this object.

Javascript:

            $.ajax({
                data: data.model,
                cache: false,
                type: 'post',
                url: 'Respondents/DownloadCSV',
            });

Notice the data.model javascript object looks like so (taken from console.log):

[Object, Object, Object, Object, Object]
0: Object
1: Object
2: Object
3: Object
4: Object

Inside the 0:Object

Class: "respondent clickable gradeA"
Data: Object
Age: ""
City: ""
Email: null
Ethnicity: ""
Gender: ""
Id: 260619
LastActionDate: "<span class="dn">20131008175555</span>10/8 5:55 PM"
LastRecruiter: "Ben Miles"
Name: "Jerry Keys"
OwningRecruiter: "Ben Miles"
RecruitingGroup: "Competitive Users"
Source: "<span style="display:none;" >Database</span><i class="tipN icon-tasks" original-title="Database"></i>"
State: ""
Status: "<span style="display:none;" >Completed</span><i class="tipN icon-check" original-title="Completed"></i>"
class: "respondent clickable gradeA"
created: 1386790341009
url: "/Projects/644/Respondents/260619/Overview"

I am not sure what my C# method would have to look like? I would expect something like?

public ActionResult DownloadCSV(object model)
{
}

All I want to do is use the data from JavaScript in a controller method.

Upvotes: 0

Views: 1162

Answers (3)

Alex
Alex

Reputation: 38509

You could create a class with properties that your json document has:

public class MyModel 
{
  //properties
}

Then, change your Action to take a list of these:

public ActionResult DownloadCSV(IEnumerable<MyModel> model)
{

}

The serializer will take care of mapping, but there are a few concerns:

LastActionDate: "2013100817555510/8 5:55 PM"

You'd want to pass in the actual date, not HTML representation, unless you want all your classes properties to be string

Upvotes: 0

JasCav
JasCav

Reputation: 34632

ASP.NET MVC provides automatic model binding from JSON notation to your Actions as long as your model matches the JSON in its format.

So, as an example, if you are posting some information about a person, your JSON would look like this...

{ "FirstName": "John", "LastName": "Doe" }

This can, of course, get considerably more complicated, but, I'm just doing this for an example.

Now, in your Action, you want to create an POCO that matches what you expect from your JavaScript so it can bind correctly. This object would look like this:

public class Person {
  public string FirstName { get; set; }

  public string LastName { get; set; }
}

Then, in your action, you do...

public class MyController {

  [HttpPost]
  public ActionResult MyAction(Person model) {
    // Do what you need to here.
  }
}

As long as your JSON aligns with the model, the binding will happen automatically.

Note, that if you need to convert your JavaScript object to JSON, then you can use the JSON.stringify(your_object) to do so and pass that to the data parameter of the ajax call.

Upvotes: 1

Todd Menier
Todd Menier

Reputation: 39289

Try this:

$.ajax({
    data: JSON.stringify(data.model),
    cache: false,
    type: 'POST',
    url: 'Respondents/DownloadCSV',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8'
});

This should work with any object of any arbitrary depth.

Upvotes: 0

Related Questions