C# 在AJAX调用中未传递参数
我正在使用以下代码C# 在AJAX调用中未传递参数,c#,asp.net-mvc,jquery,C#,Asp.net Mvc,Jquery,我正在使用以下代码 function test() { GetAttributesForSelectedControlType('Phone Number'); } function GetAttributesForSelectedControlType(questionType) { alert(questionType); $.ajax({ url: '/Wizards/GetAttributesForSelectedControlType/' + q
function test()
{
GetAttributesForSelectedControlType('Phone Number');
}
function GetAttributesForSelectedControlType(questionType) {
alert(questionType);
$.ajax({
url: '/Wizards/GetAttributesForSelectedControlType/' + questionType,
type: "GET",
contentType: "application/json",
success: function (result) {
alert('success');
}
});
}
请注意:QUESTIONTYPE是字符串值,而不是任何类型
问题是,在控制器中,我在“GetAttributesForSelectedControlType”
函数上获得了成功,但参数值为空。
我正在questionType
中发送字符串。
有什么想法吗?如果您希望将问题类型作为参数传递,则应使用
数据:{qType:questionType}
这将填充函数GetAttributesForSelectedControlType
的参数qType,尝试:
function GetAttributesForSelectedControlType(questionType) {
alert(questionType);
$.ajax({
url: '/Wizards/GetAttributesForSelectedControlType',
contentType: "application/json",
data: {
questionType: questionType
},
success: function (result) {
alert('success');
}
});
}
您需要以数据形式传入questionType
。或者,您可以将以下内容添加到现有的ajax调用中
data: {questionType: questionType }
这将与以下操作一起工作:
public ActionResult GetAttributesForSelectedControlType(string questionType)
{
// ...
}
您在alert中获得的值是否正确?向我们显示从何处传递参数的代码,即如何调用函数。@Amit Yes alert中的值正是我所需要的。此外,方法签名/路由设置可能会有所帮助。将其用作querystring。然后,您将获得value它是一个简单的字符串还是另一个对象并不重要。理想的情况是,如果questionType有任何值,你甚至不应该用你正在做的事情来攻击控制器。上面列出的任何解决方案都是正确的方法。
public ActionResult GetAttributesForSelectedControlType(string questionType)
{
// ...
}