c# - 从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; }
}
解决方案
在您的 ajax 请求中,您应该JSON.stringify(Object);
data: JSON.stringify({key: key, model:model}),
推荐阅读
- r - ggalluvial:增加每个类别之间的空间?
- c++ - 错误:重载函数的调用不明确
- javascript - 使用 jQuery 发布 JSON 后,将字符串转换回 JSON
- c - 为什么预处理器禁止宏粘贴奇怪的标记
- java - 查询关键字 Containing、IsContaining、Contains 之间的区别,就像 Spring Data JPA 中一样
- python - 正则表达式 python。此代码无法读取仅用户名为“-”的第二行
- python - 过滤多个条件并创建新的 csv
- c# - Revit:尝试在事务之外修改模型
- javascript - 如何转换响应式导航栏并将图标更改为 X?
- c# - 如何在 C# 的抽象类中对内部方法或私有方法进行单元测试?