0

1 つは検索パラメーターを設定し、もう 1 つは検索結果用です。

検索モデル:

 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() },

実行すると、次のエラーが表示されます: *{"メッセージ":"無効な JSON プリミティブ: SearchType."、"StackTrace":" System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() で\r\n System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal( Int32 depth)\r\n System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String 入力、Int32 depthLimit、JavaScriptSerializer シリアライザー)\r\n System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer シリアライザー、String) input, Type type, Int32 depthLimit)\r\n System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](文字列入力)\r\n System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context) 、JavaScriptSerializer シリアライザー)\r\n System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}*

4

1 に答える 1

1

文字列ではなく、値を持つデータオブジェクトとして渡す必要があります。

これは次のようになります。

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

また、観察可能であるため、関数として呼び出す必要があります。

また、FireBugまたは他のコンソールツールで、JSONによってサーバーに送信される内容を確認する必要があります。

お役に立てば幸いです

于 2013-02-11T08:07:13.497 に答える