Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing objects inside query string in MVC 4 and Backbone.js

I am using backbone.js to send a GET request and pass model as parameter.

I have my model as below:-

class Vehicle
{
  public int Id
  public string Name
}
class Car
{
  public string Type
  public Vehicle Vehicle
}

Now, I have my controller as :-

    [HttpGet]    
    public ActionResult GetClasBDetails(Car carModel){
          // Something goes here
    }

When I do :-

this.model.fetch({
    data: $.param({//I have tried removing  $.param also
        Vehicle: {
            Id: '1',// Also tried '1' and 1(as numeric)
            Name:'ford mustang'
        },
        Type: "Ford"
    }),

    success: function (data) {


    }

});

When I run the above code Type property is getting mapped and I am getting proper values but not for Vehicle. Any idea?

I have to send an object in HttpGet request, because of some complications please ignore that.

EDIT:-

The URL looks like

http://localhost/Home/GetClasBDetails?Vehicle%Id%5D=10&Vehicle%Name%5D=Bed+10&Type=1
like image 379
Shubh Avatar asked Nov 26 '25 12:11

Shubh


1 Answers

As your model contains a nested Vehicle object your request must be in a certain format for the Model Binder to instantiate it in your controller action.

Your request url needs to be in the following format:

http://localhost/Home/GetClasBDetails?Type=1&Vehicle.Id=1&Vehicle.Name=aName

Take particular notice to the way the nested Vehicle object is represented as the Property name separated by a period.

You need to make sure that the backbone request matches this, I have not tested this but I'm guessing that this should work:

data: {
        Type: "Ford",
        'Vehicle.Id' : 1
        'Vehicle.Name' : 'ford mustang'
    }
like image 93
hutchonoid Avatar answered Nov 29 '25 05:11

hutchonoid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!