4

我已经尝试了几乎所有方法来使用 ajax 将表单发送回我的控制器。

我已将模型简化为字符串。

  • ajax 正在正确提取所有表单数据。
  • 我也有 serilaizeArray() 。

我的名为模型的对象每次都是空的(其他参数映射就好了,即页面,排序......)。我错过了什么?

阿贾克斯:

...
var model = $('#advancesearchform').serialize();
var request = $.ajax({
    type: "POST",
    url: "/DAM/Home/_ImageSearchResult",
    cache: false,
    traditional: true,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    data: JSON.stringify({
        page: page,
        itemsperpage: itemsperpage,
        sort: sort,
        sortdir: sortdir,
        model: model
    }),
    success: function (data) {
        $('#imagesearchresults').html(data);
    }
});

JSON.stringfy :
{\"page\":null,\"itemsperpage\":8,\"sort\":\"Project\",\"sortdir\":\"ASC\",\"model\ ":\"FileName=123&OriginalFileName=sas&Height=asas&Width=asas&DepartmentId=9b4463cd-c184-e211-9244-005056887208&ClassId=28de9d15-c284-e211-9244-005056887208\"}

控制器:

[HttpPost]
public PartialViewResult _ImageSearchResult(int? page, int itemsperpage, string sort, string sortdir, AdvanceSearchFilters model)
{
}

模型:

public class AdvanceSearchFilters
{
    public string FileName { get; set; }
    public string OriginalFormat { get; set; }
    public string Width { get; set; }
    public string Height { get; set; }
    public string MediaSource { get; set; }
    public string DepartmentId { get; set; }
    public string ClassId { get; set; }
    public string ThemeId { get; set; }
}
4

3 回答 3

1

The problems is that you are sending data in the incorrect format. When you call $('#advancesearchform').serialize(); the data is formatted as a query string, but you are telling ajax to send JSON. With this:

{
 "page":null,
 "itemsperpage":8,
 "sort":"Project",
 "sortdir":"ASC",
 "model":"FileName=123&OriginalFileName=sas&Height=asas&Width=asas&DepartmentId=9b4463cd-    c184-e211-9244-005056887208&ClassId=28de9d15-c284-e211-9244-005056887208"
} 

You are sending "model" as a string. What you need to do is either convert your advancesearchform atributes to a json format and attach them to the current JSON or send everything as a query string.

For the first one, a quick soulution would be

$.ajax({
  type: "POST",
  url: "/DAM/Home/_ImageSearchResult",
  cache: false,
  traditional: true,
  contentType: 'application/json; charset=utf-8',
  dataType: 'json',
  data: JSON.stringify({
    page: page,
    itemsperpage: itemsperpage,
    sort: sort,
    sortdir: sortdir,
    model: {
        FileName: $("#FileName").val(),
        OriginalFormat: $("#OriginalFormat").val()
        and so on...

for the second one, you could do something like

$.ajax({
  type: "POST",
  url: "/DAM/Home/_ImageSearchResult",
  data: $('#advancesearchform').serialize() + "&page=" + page + "&itemsperpage=" + itemsperpage + "&sort=" + sort + "&sortdir=" +sortdir
于 2014-05-28T21:57:51.570 回答
0

查看您的代码,AdvanceSearchFilters该类有一个名为的属性OriginalFormat,而序列化表单的值为OriginalFileName

另外我建议从非 ajax 方法开始。如果您可以在非 js 环境中正确回发值,那么您就知道问题与 ajax 相关。

如果元素上的名称值不正确,则模型绑定器无法正确映射它们。例如AdvanceSearchFilters.Height,相对于Height

于 2013-03-19T10:37:28.740 回答
0

首先,尝试查看这里:JSON.Stringify 示例
或者,您可以使用 $("form").serialize() 来序列化您的数据。

$.ajax({ 
url: "....",
method: "POST",
data: $("form").serialize()
})
于 2013-10-05T23:27:28.640 回答