【问题标题】:.Net MVC Pass JSON To Controller Via AJAX.Net MVC 通过 AJAX 将 JSON 传递给控制器
【发布时间】:2017-03-23 17:05:19
【问题描述】:

这是我的模型:

public class Inspection
{
    public string InspectionId { get; set; }
    public string Description { get; set; }
}

public class InspectionReasons : Inspection
{
    public string ReasonId { get; set; }
    public string ReasonDesc { get; set; }
    public string NotUsedInUpdate { get; set; }
    public bool CheckedFlag { get; set; }
}

我从动态构建的表中获取这些数据并通过 AJAX 调用对其进行更新:

table.rows().every(function (rowIdx, tableLoop, rowLoop) {
    var data = this.data();
    var row = this.node();
    var checked = $(row).find('input').prop('checked');

    reasons.push({
        InspectionId: data['id'],
        Description: data['desc'],
        ReasonId: data['reasonId'],
        ReasonDesc: data['reasonDesc'],
        CheckedFlag: checked,
        NotUsedInUpdate: ""
    });
});

$.ajax({
    type: 'POST',
    url: '/InspectionReason/UpdateInpsectionReasons',
    dataType: 'json',
    data: {
        reasons: JSON.stringify(reasons)
    }
});

这是我的行动:

[HttpPost]
public JsonResult UpdateInpsectionReasons(List<InspectionReasons> reasons)
{
    UpdateReasons(reasons);
    return Json(string.Empty);
}

当我调试时List&lt;InspectionReasons&gt; reasons 是空的。我已经提醒了我的客户端原因对象,它是有效的 JSON。属性是否需要与它们在模型中出现的顺序相同?如果是这样,继承的属性是否低于其他属性?我看到很多这样的例子,但我似乎无法让我的工作。

【问题讨论】:

    标签: jquery json ajax asp.net-mvc


    【解决方案1】:

    缺少 Ajax contentType。例如contentType: "application/json; charset=utf-8",

    $.ajax({
        type: 'POST',
        url: '@Url.Action("UpdateInpsectionReasons", "InspectionReason")',
        dataType: 'json',
        data: JSON.stringify(reasons),
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            console.log(result);
        }
    });
    

    工作代码

    public class InspectionReasonController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        [HttpPost]
        public JsonResult UpdateInpsectionReasons(List<InspectionReasons> reasons)
        {
            return Json(reasons);
        }
    }
    

    查看

    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <button id="btnSubmit" type="button">Submit</button>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script>
            var reasons = [];
    
            $("#btnSubmit").click(function () {
                reasons.push({
                    InspectionId: "Test InspectionId",
                    Description: "Test Description",
                    ReasonId: "Test ReasonId",
                    ReasonDesc: "Test ReasonDesc",
                    CheckedFlag: true,
                    NotUsedInUpdate: "Test NotUsedInUpdate"
                });
    
                $.ajax({
                    type: 'POST',
                    url: '@Url.Action("UpdateInpsectionReasons", "InspectionReason")',
                    dataType: 'json',
                    data: JSON.stringify(reasons),
                    contentType: "application/json; charset=utf-8",
                    success: function(result) {
                        console.log(result);
                    }
                });
            });
        </script>
    </body>
    </html>
    

    【讨论】:

    • 这让我更进一步!我现在收到错误“无效的 JSON Primative:原因”。我将控制器从 JsonResult 更改为 ActionResult 类型。这与新的错误有什么关系吗?它在返回任何东西之前就失败了,所以我看不出它会怎样。
    • 在 AJAX 调用中。它甚至不属于我的公开 ActionResult。我正在努力让我的实际 JSON 对象显示我传入的内容。
    • [{"DisasterNum":"9001","RegId":"150304791","InspectionSeq":"2","ReviewReasonId":"159","Checked":true,"ReviewDesc ":"","PriorityId":"","PriorityDesc":"","Sort":""}] 这是我的实际 json。我知道它与我的通用示例不匹配,但它看起来对我有效。我会用你给的东西工作,看看我想出了什么。
    • 我会被诅咒的。它似乎是有效的 JSON,但是当我复制到 Visual Studio 时,VS 抱怨 : 之一是一个特殊字符。这是screen shot
    • 我能够让您的示例发挥作用。我将从那里向后工作,并且在我的断开连接的地方很好。谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 2013-10-13
    • 2017-09-30
    • 2011-12-21
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多