2

我在使用 ASP.NET MVC3、AJAX 和 JQUERY 时遇到了问题。我有以下功能

[HttpPost]
public bool Update(int id, FormCollection collection)

这是我的 jQuery 源代码:

$(document).ready(function () {
    $('#btnUpdate').click(function (e) {
        // Cancel default action
        e.preventDefault();
        var formCollection = $('#formId').serialize();
        $.ajax({
            cache: false,
            type: 'POST',
            url: '@Url.Action("Action","Controller")',
            data: { id: $('#id').val(), collection: formCollection },
            success: function (data) {
                alert(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error during process: \n' + xhr.responseText);
            }
        });
    });
});

id参数提交成功,但是集合(FormCollection)包含一个数组,{[0]:10000,[1]:collection}。我无法解决问题。当我重新设计这样的解决方案时:

[HttpPost]
public bool Update(FormCollection collection)

$(document).ready(function () {
    $('#btnUpdate').click(function (e) {
        // Cancel default action
        e.preventDefault();
        $.ajax({
            cache: false,
            type: 'POST',
            url: '@Url.Action("Action", "Controller")',
            data: $('#formId').serialize(),
            success: function (data) {
                alert(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error during process: \n' + xhr.responseText);
            }
        });
    });
});

一切正常。我在传递 2 参数时做错了什么?

谢谢!!!

4

4 回答 4

1
$(document).ready(function () {
    $('#btnUpdate').click(function (e) {
        // Cancel default action
        e.preventDefault();
        var formCollection = $('#formId').serialize();
        $.ajax({
            cache: false,
            type: 'POST',
            url: '@Url.Action("Action","Controller")',
            data: JSON.stringify({ id: $('#id').val(), collection: formCollection }),
            success: function (data) {
                alert(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error during process: \n' + xhr.responseText);
            }
        });
    });
});

您应该必须使用 jsonstringify 传递数据

于 2012-09-25T06:30:15.583 回答
1

尝试在 JSON 上调用 JSON.stringify():

data: JSON.stringify({ id: $('#id').val(), collection: formCollection })
于 2012-09-25T06:28:17.240 回答
0

这是真正的手表: 调试

参数 ID 提交成功(Name=id, Wert=10000)。在 id 上方,您可以看到 formCollection,包括 ajax 调用的 FORM.serialize() 值。但是 System.Web.Mvc.FormCollection 只包括两个键:idcollection!我期望

  1. “姓名”
  2. “地址”
  3. “地址2”
  4. ...

我想如果在创建我的 ajax 请求时出错,但我不知道为什么......

var customerNo = $('#fieldID').val();
var formCollection = $('#formID').serialize();
$.ajax({
    cache: false,
    type: 'POST',
    url: '@Url.Action("Action", "Controller")',
    data: { id: customerNo, collection: formCollection },
    dataType: 'json',
    success: function (data) {
        if (data) {
            alert(data);
        };
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert('Error during process: \n' + xhr.responseText);
    }
});

以下行生成错误:

data: JSON.stringify({ id: customerNo, collection: formCollection })

参数字典包含一个不可为空类型“System.Int32”的空条目“id”...

更改无效:

data: { id: customerNo, collection: JSON.stringify(formCollection) },

有任何想法吗?

于 2012-09-26T06:41:28.217 回答
0

这是我的想法(有效!)

在模型的部分类中添加字符串属性,在“数据”中手动添加此属性。

JS:

$.ajax({
        cache: false,
        type: 'POST',
        url: '@Url.Action("Action", "Controller")',
        data: $("form").serialize() + "&id=whatever",
        dataType: 'json',
        success: function (data) {
                if (data) {
                        alert(data);
                };
        },
        error: function (xhr, ajaxOptions, thrownError) {
                alert('Error during process: \n' + xhr.responseText);
        }
});

partialClass.cs 模型:

public partial class TableName
{
    public string id { get; set; }
}

控制器:

[HttpPost]
public bool Update(FormCollection collection){
    var ID = collection.id; 
}

问候!

于 2013-11-15T04:34:52.530 回答