【问题标题】:Error while sending ajax request in asp.net在 asp.net 中发送 ajax 请求时出错
【发布时间】:2015-05-13 07:31:59
【问题描述】:

我的客户端代码发送 ajax 请求

var taskDetail = {
            Id: id,
            StatusTo: "Completed", //should come from list of status to change 
            Description: description,
            Documents: document,
            Visibility: visibility
        };

        $.ajax({
            url: url_link.task_complete + JSON.stringify(taskDetail),
            type: "GET",
            success: function (e) {

                $('#Task-Status').html(e.Status);
                $('.desc-entry').hide();
                $('.desc-note').find('span').html(e.Description);
                $('#Task-Completion').html(DateHelper(e.CurrentCompletionDate));
                $('.comp-date').show();
                $('.desc-note').show();

                /*
                 * Change status
                 * hide text area
                 * show description
                 * hide complete button
                 * 
                 */

                $('.thumb').find('ul').empty();
                $('#description').val('');


                $('.desc-button').hide();
                updateTaskDocument(taskDetail.Id);
                //updateProjectPoint(taskDetail.Id);
            },
            error: function (xhrRequest, textStatus, errorThrown) {
                alert("Error while sending request");
            }
        });

我的服务器端代码

[AcceptVerbs("GET")]
    [Route("status")]
    public IHttpActionResult ChangeTaskStatus(string task)
    {
        ChangeStatusViewModel status = JsonConvert.DeserializeObject<ChangeStatusViewModel>(task);
        int result = repo.ChangeStatus(status);
        if (result < 1)
        {
            return NotFound();
        }

       // mail.Send(db.GetUserList(db.GetGroup(status.Id)));

        var detail = repo.GetTaskDetail(status.Id);
        return Ok(detail);
    }

遇到错误

Newtonsoft.Json.dll 中出现“Newtonsoft.Json.JsonSerializationException”类型的异常,但未在用户代码中处理

附加信息:

将值 {null} 转换为类型“System.Int32”时出错。路径“Documents[1]”,第 1 行,位置 107。

请帮帮我

【问题讨论】:

  • 错误清楚地提到ChangeStatusViewModel中的某些字段是int,但在ajax请求中发送的值为null。查看您发送到服务器的数据,唯一有问题的字段可能是 ID 字段。检查它是否实际上包含任何值或尝试将ChangeStatusViewModel 的Id 字段设置为int?
  • 但这是我的模型 public class ChangeStatusViewModel { public int Id { get;放; } 公共字符串 StatusTo { 获取;放; } 公共字符串描述 { 获取;放; } 公共列表 文档{ 获取;放; } 公共布尔可见性 { 获取;放; } }
  • 是的,但是当 JsoonSerialization 尝试将 json 对象反序列化为 .net 对象并且 .net 类型的所有属性都不可为空时。你会得到 htis 异常。尝试将public int Id {get;set;} 更改为public int? Id {get;set;}

标签: asp.net json


【解决方案1】:

MVC 模型绑定将为您完成工作。您可以不使用“JSON.stringify”提交数据,并确保“taskDetail”中提交的数据与“ChangeStatusViewMode”相同

$.ajax({
    url: url_link.task_complete,
    data : taskDetail,
    type: "GET",
    dataType : 'json',
    success: function (e) {
    }
});

public IHttpActionResult ChangeTaskStatus(ChangeStatusViewModel  taskDetail)
{}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2018-01-19
    • 1970-01-01
    相关资源
    最近更新 更多