简体   繁体   English

使用javascript而不是C#的ASP.net Web表单访问数据库

[英]ASP.net web form access database using javascript instead of C#

I have an online shopping application where I have product categories. 我有一个在线购物应用程序,其中有产品类别。 I have the following method to retrieve the categories from the database and it is written in C#. 我有以下方法从数据库中检索类别,它是用C#编写的。

public IQueryable<Category> GetProductCategories()
    {
        var _db = new BundleShop.Models.ProductContext();
        IQueryable<Category> query = _db.Categories;
        return query;
    }

Can anybody give me a hand writing the same code in javascript? 有人可以帮我用JavaScript编写相同的代码吗?

You cannot make a javascript call to the database. 您无法对数据库进行JavaScript调用。 The solution you are probably looking for is implementing a web service that exposes this functionality and call the webservice from the javascript (via ajax call). 您可能正在寻找的解决方案是实现公开此功能并从javascript(通过ajax调用)调用该Web服务的Web服务。

You can call with javascript one server side function that query the database. 您可以使用javascript调用查询数据库的一个服务器端函数。

server side (maybe in your home.aspx page) 服务器端(也许在您的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();;
}

define your MyEntityType Type 定义您的MyEntityType类型

public Class MyEntityType {
    public int Order;
    pubic string Description;
}

script side 脚本端

$.ajax({
   type: "POST",
   url: "/home.aspx/GetProducts",
   success: onsuccess,
   dataType: 'json',
   error: onerror
 });

manage results in success jquery function 管理成功jquery函数的结果

function onsuccess(data, status, xmlHttpRequest) {
      //manage data variable here (cycle its elements, put them in page)             
}

summarizing : 总结

  1. Create server side function to query data 创建服务器端函数以查询数据
  2. Create script function to call server side function 创建脚本函数以调用服务器端函数
  3. Manage json results in success jquery ajax call 管理json结果成功执行jquery ajax调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.net C#Web表单Web应用程序中自动访问和关闭数据库 - Access and close the database automatically in ASP.net C# web form web application ASP.NET C# Web 表格 - ASP.NET C# Web Form 使用Access数据库ASP.NET C#出错 - Getting error using access database asp.net c# ASP.NET Core 2.1,C#应用程序,C#Web服务,访问SQL数据库 - asp.net core 2.1, C# app, c# web service, access SQL database 使用asp.net C#Javascript使用Finerprint登录到Asp.net Web应用程序 - Login into Asp.net Web application with Finerprint using asp.net C# Javascript 在 ASP.NET C# Web 项目中使用任何 MS Access 数据库添加 SqlDataSource 时,Visual Studio 2022 崩溃 - Visual Studio 2022 Crashes when Adding SqlDataSource using any MS Access Database in ASP.NET C# Web Project 具有访问数据库的C#asp.net应用程序中Web表单的loginValidation - loginValidation for a web form in C# asp.net application with access db 让ASP.NET C#Web表单访问计算机上的多个文件 - Have ASP.NET C# web form access multiple files on computer 在ASP.NET Web表单中使用C#将Web表单内容转换为PDF - Convert web form content to PDF using c# in asp.net web forms 无法使用asp.net和C#访问Microsoft Access数据库 - Unable to access the Microsoft Access database using asp.net and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM