2012-12-17 63 views
2

我想通过jQuery的ajax调用发送一些数据。我的问题是:如何在我的Insert.cshtml文件中获得此JSON数组?我试过Request["rows"],Request[0][rows]等,但没有任何成功。从jQuery中检索JSON数组在Ajax中调用asp.net网页

在这里,我要发送的数据是这个(表格多行数据):

[ 
    { 
     "sl": "1", 
     "tname": "Gardening", 
     "ttype": "4", 
     "tduration": "12" 
    }, 
    { 
     "sl": "2", 
     "tname": "Nursing", 
     "ttype": "4", 
     "tduration": "45" 
    } 
] 

jQuery代码:

$.ajax({ 
    type: "POST", 
    url: "/Insert", 
    data: rows, 
    contentType: "application/json", 
    success: function (data, status) { 
     alert(status); 
    }, 
    error: function (xhr, textStatus, error) { 
     var errorMessage = error || xhr.statusText; 
     alert(errorMessage); 
    } 
}); 

更新:在的jsfiddle的部分演示 - http://jsfiddle.net/rafi867/gprQs/8/

+0

也许'data [0] ['tname']'? – qwerty

+0

@qwerty我试图访问引用页面(在这种情况下为Insert.cshtml)中的值,而不是在当前页面中。无论如何,我想你的建议会带来像“名称数据”在当前上下文中不存在的错误。 – lazylionking

+0

你不会碰巧有一个链接到您的网站,是吗?还是一个jsfiddle? – qwerty

回答

0

我已经把示例代码用于显示如何显示数组ite通过使用jQuery和CSS在网页上的ms。通过使用此尝试来理解概念,然后将其应用于您的方案。

HTML

<div class="dialog" id="unpaid-dialog" data-width="550" data-title="Checkout"> 
     <ul> 
      <li id="serviceAndRetailDetails" style="text-align: left;"></li> 
     </ul> 
    </div> 

jQuery的

<script type="text/javascript"> 


    var toBePaidItems = new Array();//create a array 
    var make = ""; 

    $.ajax({ 
     type: "GET", 
     url: "/Portal/GetServiceAndRetailSalesDetails", 
     dataType: 'json', 
     contentType: "application/json; charset=UTF-8", 
     data: { invoiceOrSaleId: invoiceOrSaleId, providerKey: providerKey }, 
     success: function (response) { 
      make = "<table id='tblPayment'>"; 
      toBePaidItems = []; 

      $.each(response, function (index, sr) { 

       make += "<tr id=" + sr.AllocationOrInvoiceOrSaleId + " class=" + sr.Class + ">" + "<td style='padding-right:100px'>" + sr.Name + "</td><td class='colTotal' style='padding-right:45px'>" + '$ ' + sr.Price.toFixed(2) + "</td><td></tr>"; 

       //insert into array 
       toBePaidItems.push(sr.AllocationOrInvoiceOrSaleId); 
      }); 

      make += "</table>"; 
      $("#serviceAndRetailDetails").html(make); 
     } 
    }); 

</script> 

控制器的操作方法

[HttpGet] 
public JsonResult GetServiceAndRetailSalesDetails(Guid invoiceOrSaleId, string providerKey) 
     { 

      var items = new List<CheckOutServiceAndRetailItem>(); 

      var serviceDetails = Repository.GetAllPayableItems(invoiceOrSaleId).ToList(); 

      foreach (var s in serviceDetails) 
      { 
       var item = new CheckOutServiceAndRetailItem 
       { 
        AllocationOrInvoiceOrSaleId = s.Allocation.AllocationId, 
        Name = s.Allocation.Service.Name, 
        Price = s.LatestTotal, 
        Class = s.Allocation.Service.IsAnExtra, 
       }; 
       items.Add(item); 
      } 

      return Json(items, JsonRequestBehavior.AllowGet); 
     } 

阵列输出将

enter image description here

如何操作数组Here

希望这将帮助你。

+0

谢谢,但这并不能真正帮助我。我想在MVC中你可以使用一个函数来接收数据;我的问题是如何为asp.net网页做类似的事情。 – lazylionking

+0

@lazylionking从您发送数据的地方,是从Insert.cshtml还是从另一个网页?你能稍微解释一下这个场景吗? – Sampath

+0

我有一个页面(假设,Default.cshtml)与一个窗体,其中用户可以添加新的行,如果需要,最后,如果他们按提交,我希望所有的数据(由多行组成)插入到数据库。由于我无法使用纯html访问动态添加的行,因此我使用jQuery将所有行打包到json对象中,并将其传递给Insert.cshtml文件,该文件应该处理数据库任务。但是我无法访问Insert.cshtml文件中的json对象,这是我的问题。对于部分概述,看看这个小提琴:http://jsfiddle.net/rafi867/gprQs/8/ – lazylionking

0

数据参数是指名称映射值对查询字符串参数,你可以通过docs告诉:

数据

数据被发送到服务器。如果 不是字符串,它将被转换为查询字符串。它附加到GET请求的url。请参阅 processData选项以防止此自动处理。 对象必须是 是键/值对。如果value是一个数组,jQuery将基于传统设置 (下面描述)的值使用相同密钥对多个 值进行序列化。

如果你再继续读了​​.param()方法的文档,这将帮助你理解这是怎么回事你的要求,以及如何jQuery的发送你的对象到服务器。

您可能只需将对象/数组命名为data对象,以便您可以在Request.Forms对象中引用它们。

1

我试图模仿你的问题在App_Code文件创建Sample.cs类:

public class Sample 
{ 
    public string sl { get; set; } 
    public string tname { get; set; } 
    public string ttype { get; set; } 
    public string tduration { get; set; } 
} 

现在你Insert.cshtml文件应该是这样的:

@{ 
    var sample = new Sample[]{ 
    new Sample{ sl = "1", tname = "Gardening", ttype = "4", tduration = "12" }, 
    new Sample{ sl = "2", tname = "Nursing", ttype = "4", tduration = "45" } 
    }; 
    Json.Write(sample, Response.Output); 
} 

和文件(ReadSample ?.cshtml),它保存你的采样对象应该是:

<!DOCTYPE html> 

<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title></title> 
     <script src="~/Scripts/jquery-1.4.2.min.js" type="text/javascript"></script> 
     <script type="text/javascript"> 
      $.getJSON('/Insert', function (sample) 
      { 
       var custList = ""; 
       $.each(sample, function (index, obj) { 
         custList += "<li>" + obj.sl + " - " + obj.tname + 
           " - " + obj.ttype + " - " + obj.tduration + "</li>"; 
       }) 
       $("#list").html("<ul>" + custList + "</ul>") 
      }) 
     </script> 
    </head> 
    <body> 
     <div id="list"></div> 
    </body> 
</html> 

在我的例子我有REA DED对象数组

$.getJSON('/Insert', function (sample) 

并创建了一个无序列表以显示其内容

$.each(sample, function (index, obj) { 
     custList += "<li>" + obj.sl + " - " + obj.tname + 
       " - " + obj.ttype + " - " + obj.tduration + "</li>"; 
}) 
$("#list").html("<ul>" + custList + "</ul>") 

我希望这可以帮助。