对从jQuery ajax检索的对象使用ForEach

时间:2015-08-19 07:03:59

标签: c# jquery asp.net ajax webmethod

我通过ajax传递一组值,然后我无法通过foreach过程从webmethod中检索这些值。 我的集合变量是var Leave = {" Date":[]," Half":[]}; 将离开传递给webmethod,但无法检索值 。请帮助我。 我的代码是

empid = document.getElementById("hdnemployee").value;
if (empid != "") {
var Leave = { "Date": [], "Half": [] };
$('.leave_item').each(function () {
var cur = $(this);
var thisdate = cur.find('.txtdate').val();
Leave.Date.push(thisdate);                   
if (cur.find('.ckbhalfday').is(':checked'))                  
   Leave.Half.push(1);
else
   Leave.Half.push(0);                 
});
var arr = new Array();
console.log(Leave);         
arr[0] = document.getElementById('drpLeavetype').value;
//arr[1] = document.getElementById('TxtDatefrom').value;
//arr[2] = document.getElementById('TxtDateTo').value;
arr[3] = document.getElementById('Txtnumdays').value;
arr[4] = document.getElementById('txtDiscription').value;
if (arr[4] != "")
{
    $.ajax({
         type: "POST",
         dataType: "json",
         contentType: "application/json",
         url: "Applyleave.aspx/saveleaveApply",
         data: JSON.stringify({ empid: empid,Leave:Leave, arr: arr }),
         success: function (msg) {
                            // document.getElementById("tblleaveapply").innerHTML = "";
                            $('#tblleaveapply').empty();
                            alert(msg.d);
                            resettxtfields();
                            BindLADetails();
                        },
                        error: function (msg) { alert(msg.d); }
                    });
                }
                else
                {
                    alert("Give Reason");
                }

            }
            else {
                alert("Select employee");
            }

的WebMethod: -

  [WebMethod]
   public static string saveleaveApply(string empid, object Leave, params string[] arr)
   {
      foreach( var c in Leave)
      {

      }
   }

1 个答案:

答案 0 :(得分:1)

除非实现IEnumerableIEnumerable<T>接口,否则您无法在对象上使用foreach语句。你需要做的是在你的代码中定义一个Type,后面会映射到这样的JSON对象: -

public class Leave
{
    public string[] Date { get; set; }
    public string[] Half { get; set; }
}

然后您可以按如下方式修改WebMethod并迭代这些项目: -

[WebMethod]
public static string saveleaveApply(string empid, Leave Leave, params string[] arr)
{
   foreach( var c in Leave.Date)
   {

   }

   foreach( var c in Leave.Half)
   {

   }
}

<强>更新
虽然,我个人不会使用这种类型,但我会使用: -

public class Leave
{
     public string Date { get; set; }
     public string Half { get; set; }
}

您需要在JS中填写此类型: -

var leave = new Array();
$('.leave_item').each(function() {
var cur = $(this);
var thisdate = cur.find('.txtdate').val();
var thishalf;
if (cur.find('.ckbhalfday').is(':checked'))                  
   thishalf = 1;
else
   thishalf = 0;   
 leave.push({ "Date": thisdate, "Half": thishalf });              
});

最后,WebMethod将如下所示: -

[WebMethod]
 public static string saveleaveApply(string empid, Leave[] Leave, params string[] arr)
 {
     foreach( var c in Leave)
     {

     }
 }