我收到 500(内部服务器错误),但我不知道为什么。 我只想将 Javascript 对象传递给 C# 函数。
这是 Javascript 函数:
function validateImpact(id_dcr) {
$.ajax({
type: 'POST',
url: '/ModifyDocument/ImpactAssesment',
dataType: 'json',
data: {
id: 0,
quality: $("#quality").is(":checked"),
spares: $("#spares").is(":checked"),
supply: $("#supply").is(":checked"),
methods: $("#methods").is(":checked"),
mp: $("#mp").is(":checked"),
organization: $("#organization").is(":checked"),
procurement: $("#procurement").is(":checked"),
logistic: $("#logistic").is(":checked"),
health: $("#health").is(":checked"),
software: $("#software").is(":checked"),
details: $("#details").val(),
id_dcr: id_dcr
},
success: function (data) {
alert(data);
},
error: function (xhr, ajaxOptions, error) {
alert(xhr.status);
alert('Error: ' + xhr.responseText);
}
});
这是将在 JavaScript 函数中调用的 C# 方法定义:
[HttpPost]
public ActionResult ImpactAssesment(Impact impact){
//Do something
return RedirectToAction("Index", "ModifyDocument");
}
这是 C# 类名“Impact”:
public partial class impact
{
[Key]
public int id { get; set; }
[Required]
public bool quality { get; set; }
[Required]
public bool spares { get; set; }
[Required]
public bool supply { get; set; }
[Required]
public bool methods { get; set; }
[Required]
public bool mp { get; set; }
[Required]
public bool organization { get; set; }
[Required]
public bool procurement { get; set; }
[Required]
public bool logistic { get; set; }
[Required]
public bool health { get; set; }
[StringLength(1073741823)]
public string details { get; set; }
[Required]
public bool software { get; set; }
[Required]
public int id_dcr { get; set; }
[ForeignKey("id_dcr")]
public virtual dcr dcr { get; set; }
}
最佳答案
看起来您正在使用 MVC Controller (而不是 Web API),因此您应该删除 dataType: 'json'
选项
关于javascript - 内部服务器错误: Passing JavaScript object to C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35753350/