![](/img/trans.png)
[英]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.