ASP.NET MVC ModelBinder不使用GET请求和/或jQuery AJAX?

时间:2009-05-30 09:15:54

标签: jquery asp.net-mvc ajax modelbinder

当通过GET而不是POST调用时,我似乎遇到了让MVC填写我的自定义模型参数的问题。

我有一个JavaScript代码段,可以调用这样的动作:

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

它做什么,基本上是一个单独的动作,传递与调用页面相同的查询字符串。然后,如果结果为“OK”,则刷新当前页面。

该操作的定义如下:

    public ActionResult DoSearch(SearchParameters searchParameters)

模型是:

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

调用URL(使用firebug验证)与/DoSearch?Query=some+query类似。 (也试过/DoSearch?searchParameters.Query=some+query但没有成功)

无论我尝试什么,我的参数总是显示为空(非空,只是所有参数都被初始化为默认值)

如果我定义这样的动作:

    public ActionResult DoSearch(string Query, ...)

然后我的参数被正确填充。但不是模型。

我假设:

a)填充对象模型对GET请求不起作用。

b)我做错了什么

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:6)

您需要在类上绑定公共属性。

替换

public string Query;

public string Query{get;set;}

至少这是我必须要做的才能让它在我的项目中工作..我不知道你是否还有其他问题。 哦,我也使用了GET,所以它应该可以工作。

这是我的参数类:

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

没有与字段绑定。