I seem to have a problem with getting MVC to fill in my custom model parameter when called through GET instead of POST.
当通过GET而不是POST调用时,我似乎遇到了让MVC填写我的自定义模型参数的问题。
I have a JavaScript snippet that calls into an action like so:
我有一个JavaScript代码段,可以调用这样的动作:
$.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.
它的作用,基本上是一个单独的动作,传递与调用页面相同的查询字符串。然后,如果结果为“OK”,则刷新当前页面。
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)
调用URL(用firebug验证)就像是/ DoSearch?Query = some + query。 (也试过/DoSearch?searchParameters.Query=some+query但没有成功)
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.
a)填充对象模型对GET请求不起作用。
b) I'm doing something wrong
b)我做错了什么
Any ideas? Thanks.
有任何想法吗?谢谢。
1 个解决方案
#1
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.
至少这是我必须要做的才能让它在我的项目中工作..我不知道你是否还有其他问题。哦,我也使用GET,所以它应该工作。
This is my Parameters class:
这是我的Parameters类:
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.
没有绑定字段。
#1
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.
至少这是我必须要做的才能让它在我的项目中工作..我不知道你是否还有其他问题。哦,我也使用GET,所以它应该工作。
This is my Parameters class:
这是我的Parameters类:
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.
没有绑定字段。