如何将字典从ajax传递给控制器

时间:2018-01-23 11:29:13

标签: javascript jquery ajax asp.net-mvc dictionary

我想将字典从AJAX传递给控制器 所以我想通过所有输入元素来创建dictionary<element.name as TKey, element.value as TValue>

这怎么可能?

控制器

[HttpPost]
public ActionResult SearchResult(int reportId, Dictionary<string, object> elementValue)

JS

$("#btnSearch").click(function() {
  var reportId = $(this).data("id");

  var elementValues = {};
  $("#pnlFilter :input").each(function() {
    if ($(this).attr("type") === "checkbox") {
      elementValues = {
        "key": this.name,
        "value": $(this).is(":checked")
      };
      //elementValues.push(this.name + "-" + $(this).is(":checked"));
    } else {
      elementValues = {
        "key": this.name,
        "value": this.value
      };
      //elementValues.push(this.name + "-" + this.value);
    }
  });

  alert(elementValues);

  $.ajaxSetup({
    cache: false
  });
  $.ajax({
    type: "POST",
    url: "/Report/SearchResult",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({
      reportId,
      elementValues
    }),
    dataType: "json",
    success: function() {
      var url = "/Report/EditFilters?reportId=" + reportId;
      $("#pageContent").load(url);
    }
  });
});

0 个答案:

没有答案