Pergunta

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
Foi útil?

Solução

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'
    }

Outras dicas

I would agree with hutchonoid, it works only thing that needs to be done is, wrapping up the properties in single quote, else it would throw Unexpected token . error :-

data: {
        Type: "Ford",
        'Vehicle.Id' : 1
        'Vehicle.Name' : 'ford mustang'
    }

Please correct me if this is a wrong standard or anything of that sort.. Thanks @hutchonoid

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top