C# Json控制器方法中的Null参数,而Jquery参数具有值
我正在根据我找到的一个示例创建一个级联下拉列表 发送到服务器以请求第二个dropdownlist值的查询具有非null参数,但当我中断控制器方法时,它显示为空。正如你在下面看到的 任何帮助都将不胜感激!谢谢 它使用jQuery和ASP.NETMVC5,而我的项目是ASP.NETMVCCore2 控制器中的代码如下所示:C# Json控制器方法中的Null参数,而Jquery参数具有值,c#,jquery,asp.net-core-mvc,html-helper,asp.net-core-mvc-2.0,C#,Jquery,Asp.net Core Mvc,Html Helper,Asp.net Core Mvc 2.0,我正在根据我找到的一个示例创建一个级联下拉列表 发送到服务器以请求第二个dropdownlist值的查询具有非null参数,但当我中断控制器方法时,它显示为空。正如你在下面看到的 任何帮助都将不胜感激!谢谢 它使用jQuery和ASP.NETMVC5,而我的项目是ASP.NETMVCCore2 控制器中的代码如下所示: public JsonResult States(string Country) { List<string> StatesList = new List&l
public JsonResult States(string Country)
{
List<string> StatesList = new List<string>();
switch (Country)
{
case "India":
StatesList.Add("New Delhi");
StatesList.Add("Mumbai");
StatesList.Add("Kolkata");
StatesList.Add("Chennai");
break;
}
return Json(StatesList);
}
publicJSONResult状态(字符串国家)
{
列表状态列表=新列表();
交换机(国家/地区)
{
案例“印度”:
国家列表。添加(“新德里”);
州列表添加(“孟买”);
州列表添加(“加尔各答”);
国家列表添加(“钦奈”);
打破
}
返回Json(StatesList);
}
这里是AJAX:
<script src = "/lib/jquery/dist/jquery.js" > </script>
<script>
$(document).ready(function ()
{
$("#State").prop("disabled", true);
$("#Country").change(function ()
{
if ($("#Country").val() != "Select")
{
var CountryOptions = {};
CountryOptions.url = "/Dropdown/states";
CountryOptions.type = "POST";
CountryOptions.data = JSON.stringify({ Country: $("#Country").val() });
CountryOptions.datatype = "json";
CountryOptions.contentType = "application/json";
CountryOptions.success = function (StatesList)
{
$("#State").empty();
for (var i = 0; i < StatesList.length; i++)
{
$("#State").append("<option>" + StatesList[i] + "</option>");
}
$("#State").prop("disabled", false);
};
CountryOptions.error = function ()
{
alert("Error in Getting States!!");
};
$.ajax(CountryOptions);
}
else
{
$("#State").empty();
$("#State").prop("disabled", true);
}
});
});
$(文档).ready(函数()
{
$(“#State”).prop(“disabled”,true);
$(“#国家”)。更改(功能()
{
如果($(“#国家”).val()!=“选择”)
{
var CountryOptions={};
CountryOptions.url=“/Dropdown/states”;
CountryOptions.type=“POST”;
CountryOptions.data=JSON.stringify({Country:$(“#Country”).val()});
CountryOptions.datatype=“json”;
CountryOptions.contentType=“应用程序/json”;
CountryOptions.success=函数(StatesList)
{
$(“#State”).empty();
对于(变量i=0;i
由于您已经指定了
contentType=“application/json”
并正在发送字符串化数据,因此您需要在POST方法中添加[FromBody]
属性,以指示ModelBinder
使用内容类型头来确定用于读取请求的IInputFormatter
(对于json,它是JsonInputFormatter
)。将方法的签名更改为
[HttpPost]
public JsonResult States([FromBody]string Country)
但是,不必将数据作为json发送,您可以使用默认的contentType
('application/x-www-form-urlencoded;charset=UTF-8'
)。您可以删除contentType
选项并使用
CountryOptions.data = { Country: $("#Country").val() }; // not stringified
// CountryOptions.contentType = "application/json";
有关更多信息,请参阅。尝试将
[FromBody]
参数添加到方法-public JsonResult States([[FromBody]string Country)`-但没有理由将其作为应用程序/json
发送-您只需删除contentType
选项并使用CountryOptions.data={Country:$(“#Country”).val()
^^使用[HttpPost]
属性标记控制器方法。感谢@StephenMuecke确实没有发送它,因为应用程序/json
完成了工作!@Archer,[HttpPost]不起作用,但是谢谢!如果方法是[HttpPost]
并且您使用[FromBody]
属性,它应该起作用