无法将数据转换为jquery ajax中的动作参数 - ASP.Net MVC

时间:2017-01-02 08:00:52

标签: jquery ajax asp.net-mvc

我在mvc中有这样的动作:

  [HttpPost]
    public virtual ActionResult AddAttachment(Guid scheduleId, DropZoneResultViewModel file2)
    {
       // do something here
        return Json(true);

    }

我有这个js代码:

var _data = {
        scheduleId: '2867dc39-5455-10e6-4e6f-7ea197bc2f22',
        file2: {
            FileName: '12.jpg',
            OriginalFileName: '1245',
            FileSize: 12454,
            ContentType: 'image/exe',
            FileExtension: '.exe'
        }


    };


    $.ajax({
        url: addattachmneturl,
        data: JSON.stringify( _data),
                type: 'POST',
                suucess: function (data) {
                    alert('yes , you can ');
        },
                error: function (response) {
        }
    });

但它返回此错误消息:

  

参数字典包含参数' scheduleId'的空条目。非可空类型&Systems.Guid'

和_data的结果是:

{"scheduleId":"2867dc39-5455-10e6-4e6f-7ea197bc2f22","file2":{"FileName":"12.jpg","OriginalFileName":"1245","FileSize":12454,"ContentType":"image/exe","FileExtension":".exe"}}:""

1 个答案:

答案 0 :(得分:1)

查看请求有效负载 -

var _data = {
    scheduleId: '2867dc39-5455-10e6-4e6f-7ea197bc2f22',
    file2: {
        FileName: '12.jpg',
        OriginalFileName: '1245',
        FileSize: 12454,
        ContentType: 'image/exe',
        FileExtension: '.exe'
    }
};

我希望AddAttachment操作看起来像下面的那样 -

public virtual ActionResult AddAttachment(PayLoad _data){}

其中PayLoad是 -

public class PayLoad{
    public Guid scheduleId{get; set ;}
    public  DropZoneResultViewModel file2{get; set;}
}