[英]Access and close the database automatically in ASP.net C# web form web application
[英]ASP.net web form access database using javascript instead of C#
我有一个在线购物应用程序,其中有产品类别。 我有以下方法从数据库中检索类别,它是用C#编写的。
public IQueryable<Category> GetProductCategories()
{
var _db = new BundleShop.Models.ProductContext();
IQueryable<Category> query = _db.Categories;
return query;
}
有人可以帮我用JavaScript编写相同的代码吗?
您无法对数据库进行JavaScript调用。 您可能正在寻找的解决方案是实现公开此功能并从javascript(通过ajax调用)调用该Web服务的Web服务。
您可以使用javascript调用查询数据库的一个服务器端函数。
服务器端(也许在您的home.aspx页中)
[WebMethod()]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string[] GetProducts()
{
var _db = new BundleShop.Models.ProductContext();
IQueryable<Category> query = _db.Categories;
return query.Cast<MyEntityType>().ToArray();;
}
定义您的MyEntityType
类型
public Class MyEntityType {
public int Order;
pubic string Description;
}
脚本端
$.ajax({
type: "POST",
url: "/home.aspx/GetProducts",
success: onsuccess,
dataType: 'json',
error: onerror
});
管理成功jquery函数的结果
function onsuccess(data, status, xmlHttpRequest) {
//manage data variable here (cycle its elements, put them in page)
}
总结 :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.