2012-09-25 40 views
2

我有一个使用ASP.NET MVC3,AJAX和JQUERY的问题。我有以下功能ASP MVC 3使用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]:收集}。我无法解决问题。当我重新设计这样的解决方案:

[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参数时做错了什么?

THX !!!

+0

这种方式不能获得'FormCollection'你在这里传递模型类的集合定义。比如'public bool Update(int id,userProfile collection)' – Sender

+0

请确定在IE中检查了这个[是否支持IE 8的JSON.stringify()?](http://stackoverflow.com/questions/3326893/is-json- stringify-supported-by-ie-8) – Sender

回答

1

尝试在你的JSON调用JSON.stringify():

data: JSON.stringify({ id: $('#id').val(), collection: formCollection }) 
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

+0

创建以下错误:参数字典包含非空的类型'System.Int32'的空条目'id'... – tmieruch

+0

这意味着您还必须查看您的ID字段 –

+0

或使[HttpPost] 公共布尔更新(int id,FormCollection集合)到[HttpPost] 公共布尔更新(int?id,FormCollection集合) –

0

这是实际的手表传递数据: Debugging

成功提交参数ID(Name = id,Wert = 10000)。在id上方,你可以看到formCollection,包括ajax调用的FORM.serialize()值。但System.Web.Mvc.FormCollection仅包含两个键:id集合!我预计

  1. “名称”
  2. “地址”
  3. “Address_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' 的 '身份证' ...

改变,而不影响:

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

任何想法?

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; 
} 

商祺!