如何使用ajax调用将对象传递给控制器 [英] How to pass an object to controller using ajax call

查看:92
本文介绍了如何使用ajax调用将对象传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个对象传递给控制器​​,并在控制器中检索值. 我的定义如下:

I want to pass an object to controller and retrieve the values in the controller. I have defined like:

HTML代码:

 var positionarray = [];

JavaScript:

Javascript:

 $("#button").live('click',function(){
     positionarray.push({
         id: sessionStorage.getItem('id'),
         value: $("#input").val() 
     });
 });

 // on save button click
 $.ajax({
       type: "GET",                                                 
       url:"/Bugs/Position",                                                 
       data: {
           array:positionarray      
       },
       cache: false,
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function (json) {

       }
 });

但是我无法在控制器中检索值.它变得空了.

But i am not able to retrieve the values in the controller. It is getting null.

推荐答案

尝试以下操作:-您正在传递对象数组,因此您应该执行HTTPPost而不是HttpGet(这适用于原始类型的数组,例如int列表, strgin等)通过查询字符串发送(记住查询字符串的限制). 尝试使用HTTPPost

Try this:- You are passing an array of objects so you should do HTTPPost rather than HttpGet (this will work for array of primitive types say list of int, strgin etc) sending it through query string(Remember the limit for query string). Try this with HTTPPost

 $.ajax({
       type: "POST",                     
       url:"Home/Position",                                                 
       data: JSON.stringify({
              array: positionarray
       }),
       cache: false,
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function (json) {

       }

[HTTPPost]
public void Position(YourClass[] array){...

这篇关于如何使用ajax调用将对象传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆