user469652
user469652

Reputation: 51291

Pass JSON to ASP.NET MVC2 website

I've constructed a list of JSON object, and now want to pass to ASP.NET MVC2 website.

If I dont use web service, instead use Controllers/Views, what is the input type? string? What are good practices when doing this?

The purpose is that I want to use Microsoft's AJAX framework, that enables the browsers without javascript supported still able to use the website. Otherwise, I would use jQuery to make AJAX call to web service.

Upvotes: 0

Views: 2965

Answers (2)

Robert Koritnik
Robert Koritnik

Reputation: 105039

Basically things are supported quite a bit out of the box. I don't register any of the Phil Haack's things, and things work without a problem. Default model binder is able to consume data passed to the server. Getting JSON strings on the server is a no-brainer only when you have a client side (or other server side service) that you can't really control. Phil Haack explains it very well in this blog post comment.

Server-side part

Consider this server side application model:

public class Person
{
    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    [Required]
    [Range(0, 100)]
    public int Age { get; set; }
}

And consider this controller (People controller) action:

[HttpPost]
public ActionResult Add(Person data)
{
    if (!this.ModelState.IsValid)
    {
        // do something about invalid data (check "Additional Info" below)
    }

    Person result = this.Service.Add(data);
    return Json(result);
}

Client-side part

This action will be able to consume javascript objects like:

var person = {
    FirstName: "John",
    LastName: "Doe",
    Age: 35
};

By using jQuery for instance it would be this way:

$.ajax({
    url: "People/Add",
    type: "POST",
    data: person,
    success: function(data, status, xhr) {
        // process data
    },
    error: function(xhr, status, err) {
        // process invalid results
    }
});

This will not directly send JSON string to the server, but I wonder why would one bother to convert to JSON string and send that and then do additional processing on the server side, when you can use KISS principle and follow this route I've described here. It works out of the box even in MVC 1. You can pass through whatever you need. Lists, arrays, objects etc.

You've probably constructed your JSON string out of a real Javascript object/array. I'm suggesting you use this original object, you've constructed and pass it to $.ajax call.

Additional info

Handling model state errors isn't as trivial as one may think. Read my blog post to get some more information about it and one of the possible solutions to the problem.

Passing through lists or other collections may pose an additional challenge. Check a different blog post about it and you'll see how to properly do it.

Upvotes: 1

Chuck
Chuck

Reputation: 8272

Here's a good article on how to pass JSON to actions. It does require you to include the ASP.NET Futures assembly, but that's just dropping a dll into your bin and making the reference. The nice thing here is that the code will automatically bind to models for you. There is one exception I found so far: enums don't work yet. My work around involved creating a default model binder that handles converting enums from ints, though there might be better options out there.

Upvotes: 0

Related Questions