我需要在C#
[HttpPost]
public bool information([FromBody]int idInfromation,[FromBody] string information)
{
.....
return true;
}
ajax
,但我的解决方案不起作用。 Newtwork
标签显示:
POST http:// ----- / information / SubmitAnswer 500(内部服务器错误)
我有这个:
var source = { "idInfromation": 5, "information": "Wau" };
$.ajax({
url: "/information/",
type: "POST",
data: source,
dataType: "json",
contentType: "application/json",
success: function (data) {
alert("Complete");
}
});
感谢您的建议。
答案 0 :(得分:0)
您无法使用[FromBody]返回两个值。创建DTO类或将其作为JObject接收。
答案 1 :(得分:0)
您无法传递多个[FormBody]参数,而必须将参数作为对象传递。 例如,声明一个DTO类
// DTO
public class InformationClass{
public int idInfromation{ get; set; }
public string Information{ get; set; }
}
[HttpPost]
public bool information(InformationDTO Info)
{
.....
return true;
}
使用Json ajax传递对象:
// Initialize the object, before adding data to it.
// { } is declarative shorthand for new Object().
var Information= { };
Information.idInfromation= ParseInt($("txtId").val()); // your desired value
Information.Information= $("#Information").val();
// Create a data transfer object (DTO) with the proper structure.
var DTO = { 'Information' : Information};
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "InformationService.asmx/information", // or your specified url, if you don't want //use service like url: "/information/",
data: JSON.stringify(DTO),
dataType: "json"
});
希望这有帮助, 感谢。