2013-02-11 36 views
0

我有2個viewmodels,1個設置搜索參數,另外一個是搜索結果。從1 viewmodel中獲取值並在ajax中用作數據knockoutjs

搜索型號:

var CustomerSearchViewModel = { 
     SearchType: ko.observable(""), 
     SearchString: ko.observable(""), 
     setSearchType: function (data, element) { 
      this.SearchType($(element.target).val()); 
     } 
    } 

結果型號:

var CustomerSearhResultViewModelDS = function (data) { 
     var self = this; 
     self.CustomerID = ko.observable(data.CustomerID); 
     self.CompanyName = ko.observable(data.CustomerName); 
     self.CustomerEMail = ko.observable(data.CustomerEMail); 
     self.CustomerTelephone = ko.observable(data.CustomerTelephone); 
     self.CustomerCompanyName = ko.observable(data.CustomerCompanyName); 
     self.CustomerCompanyAddress1 = ko.observable(data.CustomerCompanyAddress1); 
     self.CustomerCompanyAddress2 = ko.observable(data.CustomerCompanyAddress2); 
     self.CustomerCompanyZipCode = ko.observable(data.CustomerCompanyZipCode); 
    } 

    var CustomerSearhResultViewModel = function (Customer) { 
     var self = this; 

     self.Customer = ko.observableArray(Customer); 

     $.ajax({ 
      url: "CreateOrder.aspx/CustomerSearch", 
      data: { SearchType: CustomerSearchViewModel.SearchType(), SearchString: CustomerSearchViewModel.SearchString() }, 
      type: "POST", 
      contentType: "application/json; charset=utf-8", 
      dataType: "JSON", 
      timeout: 10000, 
      success: function (Result) { 
       var MappedCustomer = 
       $.map(Result.d, 
      function (item) { 
       return new CustomerSearhResultViewModelDS(item); 
      } 
       ); 
       self.Customer(MappedCustomer); 
      }, 
      error: function (xhr, status) { 
       alert(status + " - " + xhr.responseText); 
      } 
     }); 
    }; 

我的後端代碼:

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public static List<CustomerAddress> CustomerSearch(int SearchType, string SearchString) 
    { 
     //int Converted; 
     //Int32.TryParse(SearchType,out Converted); 

     nopcommerce144Entities entities = new nopcommerce144Entities(); 
     List<CustomerAddress> json = null; 

     switch (SearchType) 
     { 
      case 0: 
       json = (from cr in entities.Addresses 
           where cr.Company.Contains(SearchString) 
           select new CustomerAddress() 
           { 
            CustomerCompanyName = cr.Company, 
            CustomerID = (from cid in entities.Affiliates 
               join cus in entities.Customers on cid.Id equals cus.AffiliateId 
               where cid.AddressId == cr.Id 
               select (int)cus.Id).First(), 
            CustomerCompanyAddress1 = cr.Address1, 
            CustomerCompanyAddress2 = cr.Address2, 
            CustomerCompanyZipCode = cr.ZipPostalCode, 
            CustomerName = cr.FirstName + " " + cr.LastName, 
            CustomerTelephone = cr.PhoneNumber, 
            CustomerEMail = cr.Email 
           } 
           ).ToList(); 

       break; 
      case 1: 
       json = (from cr in entities.Addresses 
          where cr.City.Contains(SearchString) 
          select new CustomerAddress() 
          { 
           CustomerCompanyName = cr.Company, 
           CustomerID = (from cid in entities.Affiliates 
               join cus in entities.Customers on cid.Id equals cus.AffiliateId 
               where cid.AddressId == cr.Id 
               select (int)cus.Id).First(), 
           CustomerCompanyAddress1 = cr.Address1, 
           CustomerCompanyAddress2 = cr.Address2, 
           CustomerCompanyZipCode = cr.ZipPostalCode, 
           CustomerName = cr.FirstName + " " + cr.LastName, 
           CustomerTelephone = cr.PhoneNumber, 
           CustomerEMail = cr.Email 
          } 
      ).ToList(); 

       break; 
      case 2: 
       json = (from cr in entities.Addresses 
           where cr.ZipPostalCode.Contains(SearchString) 
           select new CustomerAddress() 
           { 
            CustomerCompanyName = cr.Company, 
            CustomerID = (from cid in entities.Affiliates 
               join cus in entities.Customers on cid.Id equals cus.AffiliateId 
               where cid.AddressId == cr.Id 
               select (int)cus.Id).First(), 
            CustomerCompanyAddress1 = cr.Address1, 
            CustomerCompanyAddress2 = cr.Address2, 
            CustomerCompanyZipCode = cr.ZipPostalCode, 
            CustomerName = cr.FirstName + " " + cr.LastName, 
            CustomerTelephone = cr.PhoneNumber, 
            CustomerEMail = cr.Email 
           } 
      ).ToList(); 

       break; 
      case 3: 
       json = (from cr in entities.Addresses 
            where cr.FirstName.Contains(SearchString) || cr.LastName.Contains(SearchString) 
            select new CustomerAddress() 
            { 
             CustomerCompanyName = cr.Company, 
             CustomerID = (from cid in entities.Affiliates 
                join cus in entities.Customers on cid.Id equals cus.AffiliateId 
                where cid.AddressId == cr.Id 
                select (int)cus.Id).First(), 
             CustomerCompanyAddress1 = cr.Address1, 
             CustomerCompanyAddress2 = cr.Address2, 
             CustomerCompanyZipCode = cr.ZipPostalCode, 
             CustomerName = cr.FirstName + " " + cr.LastName, 
             CustomerTelephone = cr.PhoneNumber, 
             CustomerEMail = cr.Email 
            } 
      ).ToList(); 

       break; 
      case 4: 
       json = (from cr in entities.Addresses 
          where cr.PhoneNumber.Contains(SearchString) 
          select new CustomerAddress() 
          { 
           CustomerCompanyName = cr.Company, 
           CustomerID = (from cid in entities.Affiliates 
              join cus in entities.Customers on cid.Id equals cus.AffiliateId 
              where cid.AddressId == cr.Id 
              select (int)cus.Id).First(), 
           CustomerCompanyAddress1 = cr.Address1, 
           CustomerCompanyAddress2 = cr.Address2, 
           CustomerCompanyZipCode = cr.ZipPostalCode, 
           CustomerName = cr.FirstName + " " + cr.LastName, 
           CustomerTelephone = cr.PhoneNumber, 
           CustomerEMail = cr.Email 
          } 
      ).ToList(); 

       break; 
      case 5: 
       json = (from cr in entities.Addresses 
          where cr.Email.Contains(SearchString) 
          select new CustomerAddress() 
          { 
           CustomerCompanyName = cr.Company, 
           CustomerID = (from cid in entities.Affiliates 
               join cus in entities.Customers on cid.Id equals cus.AffiliateId 
               where cid.AddressId == cr.Id 
               select (int)cus.Id).First(), 
           CustomerCompanyAddress1 = cr.Address1, 
           CustomerCompanyAddress2 = cr.Address2, 
           CustomerCompanyZipCode = cr.ZipPostalCode, 
           CustomerName = cr.FirstName + " " + cr.LastName, 
           CustomerTelephone = cr.PhoneNumber, 
           CustomerEMail = cr.Email 
          } 
       ).ToList(); 
       break; 
     } 
     return json; 
    } 

我想通過Ajax的職位從CustomerSearchViewModel的值傳遞給CustomerSearchResult用此行:

 { SearchType: CustomerSearchViewModel.SearchType(), SearchString: CustomerSearchViewModel.SearchString() }, 

當執行I得到以下錯誤: * { 「消息」: 「無效JSON原始:檢索類別。」, 「堆棧跟蹤」:」在System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() \ r \ n在System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32深度)\ r \ n在System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串輸入,Int32 depthLimit,JavaScriptSerializer序列化程序)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化程序,字符串輸入,類型類型,Int32 depthLimit)\ r \ n在System.Web.Script.Serialization.JavaScriptSerializer.Deserialize [T](字符串輸入)\ r \ n在System.Web.Script.Services.RestHandler.GetRawParamsFromPo在System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext上下文)\ r \ n上的stRequest(HttpContext上下文,JavaScriptSerializer序列化程序)\ r \ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext上下文) ,WebServiceMethodData methodData)「,」ExceptionType「:」System.ArgumentException「} *

+0

什麼是你的問題?有什麼不工作? – nemesv 2013-02-11 07:28:01

+0

我將我獲得的錯誤添加到了我的編輯中。 – 2013-02-11 07:29:48

回答

1

您應該傳遞值作爲數據對象,而不是字符串。

這應該是:

data: {CustomerSearchViewModel.SearchType(), CustomerSearchViewModel.SearchString()} 

你也應該把它作爲功能,因爲它是可觀察的。

您還應該在FireBug或其他控制檯工具中檢查通過JSON發送服務器的內容。

我希望它有幫助

+0

我現在得到這個錯誤:*** {「Message」:「無效的JSON基元:SearchType。」,「StackTrace」:「System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\ r \ n在System .Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32深度)\ r \ n *** – 2013-02-11 08:25:12

+0

什麼是發送到服務器,我知道這個錯誤是來自服務器的響應 – SiMet 2013-02-11 08:27:47

+0

SearchType = 0&SearchString = Ar,直接出來Firebug – 2013-02-11 08:31:08

相關問題