16

我正在尝试用 ajax 调用替换表单提交。该操作需要 formcollection,我不想创建新模型。所以我需要通过 ajax 调用传递整个表单(就像表单提交一样)。我尝试序列化并使用 Json,但 formcollection 为空。这是我的动作签名:

public ActionResult CompleteRegisteration(FormCollection formCollection)

这是我的提交按钮点击:

var form = $("#onlineform").serialize();              
            $.ajax({
                url: "/Register/CompleteRegisteration",                
                datatype: 'json',
                data: JSON.stringify(form),
                contentType: "application/json; charset=utf-8",                
                success: function (data) {
                    if (data.result == "Error") {
                        alert(data.message);
                    }
                }
            });

现在如何将数据传递到 formcollection?

4

3 回答 3

38

由于FormCollection是许多键值对,因此 JSON 是不适合其表示的数据格式。您应该只使用序列化的表单字符串:

var form = $("#onlineform").serialize();
$.ajax({
    type: 'POST',
    url: "/Register/CompleteRegisteration",
    data: form,
    dataType: 'json',
    success: function (data) {
        if (data.result == "Error") {
            alert(data.message);
        }
    }
});

主要变化:

  1. 设置为 POST 的请求类型(此处不需要,但看起来更自然)
  2. 序列化形式而不是 JSON 字符串作为请求数据
  3. 删除了contentType - 我们不再发送 JSON
于 2013-07-22T20:57:58.623 回答
5

尝试:

$(<your form>).on('submit',function(){
    $.ajax({
        url: "/Register/CompleteRegisteration" + $(this).serialize(), 
        // place the serialized inputs in the ajax call                
        datatype: 'json',
        contentType: "application/json; charset=utf-8",                
        success: function (data) {
            if (data.result == "Error") {
                alert(data.message);
            }
        }
    });
});
于 2013-07-22T20:54:41.120 回答
1

如果有人想将其他数据传递给 FormCollection,那么您可以在下面尝试。

<script type="text/javascript"> 
function SubmitInfo() 
{
    var id = $("#txtid").val();    
    var formData = $('#yourformname').serializeObject();
    $.extend(formData, { 'User': id }); //Send Additional data

    $.ajax({
        url: 'Controlle/GetUser',
        cache: false,
        type: 'POST',
        dataType: 'json',
        data: decodeURIComponent($.param(formData)),
        success: function (data) {
            $('#resultarea').html(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("AJAX error: " + textStatus + ' : ' + errorThrown);
        }
    });
}

$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
<script/>

动作方法

public ActionResult GetUser(FormCollection frm)
  {
   int UserId = Convert.ToInt32(frm["user"]);
   // your code
   return Json(data, JsonRequestBehavior.AllowGet);
  }

有关更多详细信息,请参阅链接

于 2016-03-30T10:39:30.127 回答