javascript - 内部服务器错误: Passing JavaScript object to C#

标签 javascript c# jquery

我收到 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/

相关文章:

Javascript 帮助改变 writeln 中的颜色

c# - C# 中的 Linq 查询和转换

JQuery UI Sortable 在 iPad 上运行良好,但列表中的正常链接现在失败

javascript - Onload 提交不起作用

javascript - jQuery 的 removeClass 方法返回什么?

javascript - float 标记函数,轴未定义?

javascript - 基于javascript数组中的多个属性进行过滤

javascript - 如何使用 glob npm 模块获取具有不同模式的文件名列表

c# - String、FormattableString、IFormattable 之间的区别

c# - 有没有办法确定如何使用 YamlDotNet 序列化 POCO 属性?