Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am fetching data from database using jquery and web service. I want to display the data that the web service returns in another view. Not in the view from which I am calling the web service. How can I do that? Please suggest any answer. Following is my code in Jquery.
C#
$('#btnSearch').click(function () {
            alert('in search function');
            $.ajax({
                type: "POST",
                url: location.pathname + "SearchFromHome.asmx/SearchFromHomeBus",
                data: "{fromid:'" + 1 + "' , toid:'" + 2 + "'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: SearchSuccess,
                error: SearchError
            });
            function SearchSuccess(data) {
                alert("you searched for ");
                var result = "<table>";
                result += "<th> " + "<td>Model</td>"
                                       + "<td>Companyname</td>"
                                       + "<td>Image</td>"
                                       + "<td>capacity</td>"
                                       + "<td>Ac/km</td>"
                                       + "<td>NonAc/km</td>"
                                       + "<td>Ac/day</td>"
                                       + "<td>NonAc/day</td>"
                + " </th>";
                for (var i = 0; i < data.d.length; i++) {
                    result += "<tr>";
                    result += "<td>";
                    result += data.d[i].model;
                    result += "</td>";

                    result += "<td>";
                    result += data.d[i].CompanyName;
                    result += "</td>";

                    result += "<td>";
                    result += "<img height='75px' width='100px' src='../../Themes/cars/" + data.d[i].CabImage + "' />"
                    //result += data.d[i].CabImage;
                    result += "</td>";


                    result += "<td>";
                    result += data.d[i].Capacity;
                    result += "</td>";


                    result += "<td>";
                    result += data.d[i].ACperkm;
                    result += "</td>";

                    result += "<td>";
                    result += data.d[i].NonACperkm;
                    result += "</td>";

                    result += "<td>";
                    result += data.d[i].ACperDay;
                    result += "</td>";

                    result += "<td>";
                    result += data.d[i].NonACperDay;
                    result += "</td>";

                    result += "</tr>";

                }
                result += "</table>";
                $("#searchResult").append(result);
                $("#SearchNewPage").html(result);
            }
            function SearchError() {
                alert("Error in search");
            }

        });

Following is my web method
C#
[WebMethod]
      public IEnumerable<CitySearchResult>  SearchFromHomeBus(int fromid, int toid)
      {
         // DbDataReader sqlreader;
          SqlParameter []para=new SqlParameter[2];
          para[0]=new SqlParameter("@city1",fromid);
          para[1]=new SqlParameter("@city2",toid);
          using (cab_4_21Entities1 d = new cab_4_21Entities1())

          {
              ObjectResult<CitySearchResult> search =d.ExecuteStoreQuery<CitySearchResult>("usp_SearchcabResult @city1, @city2", para);
              //List<CitySearchResult> search = (d.ExecuteStoreQuery<CitySearchResult>("usp_SearchcabResult", para)).ToList<CitySearchResult>();
              List<CitySearchResult> searchResult=search.ToList<CitySearchResult>();

              return searchResult;
          }
      }
Posted
Comments
Arkadeep De 30-Mar-15 9:57am    
whats the problem?
Dolly Nimavat 30-Mar-15 13:27pm    
The problem is how to redirect to the another view with the data? Or I should first redirect to another view and then from there Retrieve the data?
Arkadeep De 30-Mar-15 14:17pm    
I thing that will be better one. redirect to another view and then retrieve the data.
Dolly Nimavat 7-Apr-15 6:17am    
But I don't know how to do that using jquery and web service? Can you please help me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900