1

How to pass complex object from jquery to action controller in tag

I have a jquery code like this

function InitCell(row, column, value) {

    var MyPerson = new Object();
    MyPerson.ID = '123456789';
    MyPerson.FirstNmae = 'abc';
    MyPerson.LastName = 'def';

    var html = "<a href='/test/Index/person =" + MyPerson +"></a>";
}

In addition I have a action controller like this

public class testController : Controller

{
    //
    // GET: /test/

    public ActionResult Index(Person person)
    {
        return View();
    }

}

class Person
{
    public int ID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public List<Person> Children { get; set; }
}

3 Answers 3

3

What you can do is make an HTTP POST request instead of HTTP GET.

$('#postLink').click(function() {
    var MyPerson = {
        ID: 1234,
        FirstName: 'abc',
        LastName: 'def'
    };

    $.ajax({
        data: MyPerson,
        url: $(this).attr('href'),
        type: 'POST',
        dataType: 'json' /* this really is optional */,
        success: function (response) {
            return true;
        },
        error: function ( error ) {
            return false;
        }
    )};

    return false; /* required to stop event propagation */
});

Now you can define an HTML <a> element like so:

<a href="/test/Index" id="postLink">Ajax post the person to the server.</a>

Your controller should now be able to parse the Person object.

Edit: You might want to remove the List<Person> if you aren't passing it with each request. It would aid ASP.NET in identifying a complex type (Person in this case) as the data type of your request. It is generally better to create ViewModels for each of your views as well so that you have a strongly typed data context for your view.

class PersonViewModel
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

How do I contains the tag with a call to ajax?
You can wrap the entire JavaScript block in a click event handler. I'll update the JS code above with the example.
0

You can't, as such. URIs are strings. If you want to pass data in them that isn't formatted as a string, then you have to serialise it to a string and then deserialise at the other end.

For general purposes, JSON is a popular serialisation format, but remember that not all strings are URI safe so you will need to encode the JSON text before including it in a URI.

You just have three pieces of data though, so you could:

var html = "<a href='/test/Index/person/" +
           encodeURIComponent( MyPerson.ID ) + "/" +
           encodeURIComponent( MyPerson.FirstName ) + "/" +
           encodeURIComponent( MyPerson.LastName ) + 
           "></a>";

Or, if the data already exists on the server — just pass the ID value and then get the names from the database.

Comments

0

A querystring takes parameters not the type of object you have so you will have to pass it as a querystring with delimited &

Example :

var html = "<a href='/test/Index/person?PersonId=123456789&FirtsName=Test......

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.