ASP.NET 是一个由微软开发的用于构建 Web 应用程序的框架,而 Ext JS 是一个用于构建富客户端应用程序的 JavaScript 框架。在 ASP.NET 中使用 Ext JS 可以创建具有丰富交互性和动态性的前端界面。
ASP.NET:
Ext JS:
以下是一个简单的示例,展示如何在 ASP.NET 中使用 Ext JS 创建一个基本的网格(Grid)组件。
public class ProductsController : Controller
{
public ActionResult GetProducts()
{
var products = new List<Product>
{
new Product { Id = 1, Name = "Product A", Price = 100 },
new Product { Id = 2, Name = "Product B", Price = 200 }
};
return Json(products, JsonRequestBehavior.AllowGet);
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}<!DOCTYPE html>
<html>
<head>
<title>Ext JS Grid Example</title>
<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css">
<script type="text/javascript" src="extjs/ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function() {
Ext.define('Product', {
extend: 'Ext.data.Model',
fields: [
{ name: 'Id', type: 'int' },
{ name: 'Name', type: 'string' },
{ name: 'Price', type: 'float' }
]
});
var store = Ext.create('Ext.data.Store', {
model: 'Product',
proxy: {
type: 'ajax',
url: '/Products/GetProducts',
reader: {
type: 'json',
rootProperty: ''
}
},
autoLoad: true
});
Ext.create('Ext.grid.Panel', {
title: 'Products Grid',
store: store,
columns: [
{ text: 'ID', dataIndex: 'Id' },
{ text: 'Name', dataIndex: 'Name' },
{ text: 'Price', dataIndex: 'Price', renderer: Ext.util.Format.usMoney }
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});
});
</script>
</head>
<body></body>
</html>问题: 数据加载失败或显示不正确。
原因:
解决方法:
通过以上步骤,你可以在 ASP.NET 中成功集成 Ext JS,并创建功能丰富的 Web 应用程序。