首页 > 解决方案 > 从ajax传递参数和模型

问题描述

我正在使用 ajax 请求我将参数和模型传递给控制器​​方法,但我可以获取方法上的密钥,但是当我将值设置为 truekey时获取模型值始终为 null,当我发送它时获取模型值始终为 0traditional传统的。

这是我的ajax方法。

 $('.search-hot-destination').on('click', '.rightdesti-list > a', function () {
            var key =  $(this).parents('li').find('.countryNameList').attr('id');
            var model ={
                DestinationCityID: $(this).attr('id')
            }

            $.ajax({
                type: "GET",
                url: "/Home/destination",
                data: {key: key, model:model},
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert('hello');
                }
            })
        });

这是我的控制器方法。

      public ActionResult destination(string key, FilterDataViewModel model, string searchingkey, int skip = 0)
    {

    }

这是我的模型。

  public class FilterDataViewModel
    {
        public int ItemID { get; set; }
        public string SearchingKey { get; set; }
        public int DestinationCityID { get; set; }
        public int citykey { get; set; }
    }

标签: c#ajaxasp.net-mvc

解决方案


在您的 ajax 请求中,您应该JSON.stringify(Object);

data: JSON.stringify({key: key, model:model}),

推荐阅读