如何从action方法调用web方法 [英] How to call a web method from action method
本文介绍了如何从action方法调用web方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用jquery和web服务从数据库中获取数据。我想显示Web服务在另一个视图中返回的数据。不在我调用Web服务的视图中。我怎样才能做到这一点?请建议任何答案。以下是我在Jquery中的代码。
$(' #btSearch')。点击(功能(){
alert(' in search function') );
$ .ajax({
type: POST,
url:location.pathname + SearchFromHome.asmx / SearchFromHomeBus,
数据: {fromid:' + 1 + ',toid:' + 2 + '},
dataType: json,
contentType: application / json; charset = utf-8,
成功:SearchSuccess,
错误:SearchError
});
函数SearchSuccess(数据){
alert(< span class =code-string> 您搜索了);
var result = < table>;
result + = < th> + < td> Model< / td>
+ < td>公司名称< / td>
+ < td>图片< / 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>;
结果+ = < td>;
结果+ = data.d [i] .model;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .CompanyName;
结果+ = < / td>;
结果+ = < td>;
结果+ = < img height ='75px'width ='100px'src ='.. /../ Themes / cars / + data.d [i] .CabImage + '/ >
// result + = data.d [i] .CabImage;
result + = < / td>;
结果+ = < td>;
结果+ = data.d [i] .Capacity;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .ACperkm;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .NonACperkm;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .ACperDay;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .NonACperDay;
结果+ = < / td>;
结果+ = < / tr>;
}
结果+ = < / table>;
$( #searchResult)。append(result);
$( #SearchNewPage)。html(result);
}
函数SearchError(){
alert( 搜索错误< /跨度>);
}
});
以下是我的网络方式
[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);
使用(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>信息搜索结果= search.ToList< CitySearchResult>();
return searchResult;
}
}
解决方案
(' #btnSearch')。点击(function(){
alert(' 在搜索功能');
.ajax({
type: POST,
url:location.pathname + SearchFromHome.asmx / SearchFromHomeBus,
data: {fromid:' + 1 + ',toid:' + 2 + '},
dataType: json,
contentType: application / json; charset = utf-8,
成功:SearchSuccess,
错误:SearchError
});
函数SearchSuccess(数据){
alert(< span class =code-string> 您搜索了);
var result = < table>;
result + = < th> + < td> Model< / td>
+ < td>公司名称< / td>
+ < td>图片< / 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>;
结果+ = < td>;
结果+ = data.d [i] .model;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .CompanyName;
结果+ = < / td>;
结果+ = < td>;
结果+ = < img height ='75px'width ='100px'src ='.. /../ Themes / cars / + data.d [i] .CabImage + '/ >
// result + = data.d [i] .CabImage;
result + = < / td>;
结果+ = < td>;
结果+ = data.d [i] .Capacity;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .ACperkm;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .NonACperkm;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .ACperDay;
结果+ = < / td>;
结果+ = < td>;
结果+ = data.d [i] .NonACperDay;
结果+ = < / td>;
结果+ = < / tr>;
}
结果+ = < / table>;
( #searchResult)。append(结果);
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.
$('#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
[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;
}
}
解决方案
('#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);
这篇关于如何从action方法调用web方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文