内部服务器错误:将 JavaScript 对象传递给 C#

Internal Server Error : Passing JavaScript object to C#

本文关键字:对象 JavaScript 服务器 错误 内部      更新时间:2023-09-26

我收到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# 类名"影响":

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控制器(而不是Web api),因此您应该删除dataType: 'json'选项