Passing an object as parameter to Breeze controller action
Asked Answered
S

2

6

I'm trying to send an object as a parameter through Breeze without success.

Using the following code I can send a primitive type:

Client:

var query = EntityQuery
    .from('account/authenticate')
    .withParameters({ loginRequest: "hello" });

Server:

[BreezeController]
public class AccountController : ApiController
{
    [HttpGet]
    public LoginResult Authenticate(string loginRequest)
    {
        // String for loginRequest received successfully
    }
}

However, if I try and pass a complex type up, the param is always null:

Client:

var loginRequest = { userName: 'me', password: 'pass' };

var query = EntityQuery
    .from('account/authenticate')
    .withParameters({ loginRequest: loginRequest });

Server:

[BreezeController]
public class AccountController : ApiController
{
    [HttpGet]
    public LoginResult Authenticate(LoginRequest loginRequest)
    {
        // Object for loginRequest always null
    }
}

I believe this is in part because Breeze always uses a GET for queries. A POST might handle the serialization correctly, but I can't see any way in the Breeze API to force a POST.

If I pass up a JSON string representation of the object I can pick it up server-side, but this requires manual deserialization. I realise I could do this outside of Breeze with a standard WebAPI call, but I'm trying to keep all of my server-side calls running through the same pipeline.

Is it possible to do this?

Shroyer answered 4/4, 2013 at 15:21 Comment(0)
D
4

You may be missing a [FromUri] attribute. Any time I tried to pass a more complex object or set of parameters everything would come back as null until I added that attribute.

[BreezeController]
public class AccountController : ApiController
{
    [HttpGet]
    public LoginResult Authenticate([FromUri] LoginRequest loginRequest)
    {
        // Object for loginRequest always null
    }
}
Disproportionation answered 11/11, 2013 at 11:49 Comment(0)
M
0

Why not use ->

var loginRequest = { userName: 'me', password: 'pass' };

var query = EntityQuery
   .from('account/authenticate')
   .withParameters( loginRequest);

instead of

var loginRequest = { userName: 'me', password: 'pass' };

var query = EntityQuery
   .from('account/authenticate')
   .withParameters({ loginRequest: loginRequest });
Mervinmerwin answered 4/4, 2013 at 17:8 Comment(1)
Hi Jay, I tried this and no joy - you get the Breeze error "The 'parameters' parameter must be a 'object'".Shroyer

© 2022 - 2024 — McMap. All rights reserved.