views:

272

answers:

1

I seem to have a problem with getting MVC to fill in my custom model parameter when called through GET instead of POST.

I have a JavaScript snippet that calls into an action like so:

$.getJSON('<%= Url.Action("DoSearch") %>' + location.search,
    function(data) {
        if (data.Result == "OK") {
            location.href = location.href;
        }
    });

What it does, is basically call a separate action, passing it the same querystrings as the calling page. Then if the result is "OK", it refreshes the current page.

The action is defined like the following:

    public ActionResult DoSearch(SearchParameters searchParameters)

The model is:

public class SearchParameters
{
    public string Query;
    ...
}

Calling URL (verified with firebug) is like /DoSearch?Query=some+query. (also tried /DoSearch?searchParameters.Query=some+query with no success)

No matter what I tried, my parameter always comes up as empty (not null, just all of the parameters being initialized to their default values)

If I define the action like this instead:

    public ActionResult DoSearch(string Query, ...)

Then my parameters get filled correctly. Not with the model however.

I assume:

a) either populating the object model doesn't work for GET requests.

b) I'm doing something wrong

Any ideas? Thanks.

+5  A: 

You need public properties to bind on a class.

replace

public string Query;

with

public string Query{get;set;}

At least that's what I had to do to get it to work in my project.. I don't know if you have another problem as well. Oh and I used GET as well so it should work.

This is my Parameters class:

public class Parameters
{
    public int? page { get; set; }
    public int? pageSize { get; set; }
    public string[] columnsToDisplay { get; set; }
    public string columnToSort { get; set; }
    public bool? descending { get; set; }
}

Didn't bind with fields.

Thomas Stock
Oops, that is indeed correct. It works now. Wish there was more documentation about this. Thanks a lot.
legenden
I only found out after trial -)
Thomas Stock
Nice answer Thomas. +1 - Congratulations!
eu-ge-ne
Lack of documentation is called "convention". :)
Arnis L.