2013-02-11 78 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