gpt4 book ai didi

c# - 如何使用 Asp.net Websrvice 绑定(bind) ExtJS 网格?

转载 作者:太空宇宙 更新时间:2023-11-03 13:32:07 24 4
gpt4 key购买 nike

我已经尝试了所有可能的方法来从我的网络服务中获取 JSON 数据。我可以在 fiddler 中看到 JSON 数据。一切都很好,除了数据没有与网格绑定(bind)。这是我的代码。

网络服务 (Mywebservice.asmx)

[WebService(Namespace = "http://myServiceLink.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class SmartlinkService : System.Web.Services.WebService
{
[WebMethod(CacheDuration = AppConsts.NONE)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
public List<SampleClass> GetTasks()
{
List<SampleClass> listSampleClass = new List<SampleClass>(){
new SampleClass{TaskId="1",TaskName="task1"},
new SampleClass{TaskId="2",TaskName="task2"},
new SampleClass{TaskId="3",TaskName="task3"},
new SampleClass{TaskId="4",TaskName="task4"}
};
return listSampleClass;
}

[Serializable]
public class SampleClass
{
public string TaskId { get; set; }
public string TaskName { get; set; }
}
}

用户控件 (UserControl.ascx)

<link href="Resources/extJS/resources/css/ext-all-debug.css" />
<script src="Resources/extJS/ext-all-debug.js" language="JavaScript" />
<script src="Resources/extJS/Ext.ux.AspWebAjaxProxy.js" ResourceType="JavaScript" />
<div class="section" runat="server" id="senchaGrid">
<h1 class="mhdr">
Project Tasks
</h1>
<div class="content">
<div class="swrap">
<%--sencha grid area--%>
<div id="topic-grid">
</div>
</div>
</div>

ExtJS 脚本代码

<script type="text/javascript" language="javascript">
/**********************************Sencha Grid Code START***********************************/
Ext.Loader.setConfig({ enabled: true });

Ext.Loader.setPath('Ext.ux', '../Resources/extjs/examples/ux');
Ext.require([
'Ext.grid.*',
'Ext.data.*',
'Ext.panel.*',
'Ext.util.*',
'Ext.toolbar.Paging',
'Ext.ux.PreviewPlugin',
'Ext.ModelManager',
'Ext.layout.container.Border',
'Ext.tip.QuickTipManager'
]);

Ext.onReady(function () {

Ext.QuickTips.init();
// setup the state provider, all state information will be saved to a cookie
Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider'));
Ext.namespace('EXT');

Ext.define('myModel', {
extend: 'Ext.data.Model',
fields: ['TaskId', 'TaskName'],
id: 'TaskId'
});

var storeData = new Ext.data.Store(
{
autoLoad: true,
proxy: new Ext.ux.AspWebAjaxProxy({
url: '/ProjectManagement/Mywebservice.asmx/GetTasks',
actionMethods: {
read: 'POST'
},
reader: {
type: 'json',
model: 'myModel',
root: 'd'
},
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
});

//var pluginExpanded = true;

debugger;

var myGrid = Ext.create('Ext.grid.Panel', {
width: 1115, height: 500,
//title: 'Manage tasks',
store: storeData,
//disableSelection: true,
//stateful: true,
//stateId: 'stateGrid',
loadMask: true,
// grid columns
columns: [
{ text: 'TaskId', dataIndex: 'TaskId', header: 'Row #' },
{ text: 'TaskName', dataIndex: 'TaskName', header: 'Task Name' }
],

// paging bar on the bottom
bbar: Ext.create('Ext.PagingToolbar', {
store: storeData,
displayInfo: true,
displayMsg: 'Displaying tasks {0} - {1} of {2}',
emptyMsg: "No topics to display"
}),
renderTo: 'topic-grid'
});

// trigger the data store load
//store.load();
});

/**************************Sencha Grid Code END******************************/

Ext.ux.AspWebAjaxProxy.js

/// <reference path="/Scripts/ext-all-debug.js" />

Ext.define('Ext.ux.AspWebAjaxProxy', {
extend: 'Ext.data.proxy.Ajax',
require: 'Ext.data',

buildRequest: function (operation) {
var params = Ext.applyIf(operation.params || {}, this.extraParams || {}),
request;
params = Ext.applyIf(params, this.getParams(params, operation));
if (operation.id && !params.id) {
params.id = operation.id;
}

params = Ext.JSON.encode(params);

request = Ext.create('Ext.data.Request', {
params: params,
action: operation.action,
records: operation.records,
operation: operation,
url: operation.url
});
request.url = this.buildUrl(request);
operation.request = request;
return request;
}
});

我从服务器获取的 JSON 数据(在 Fiddler 中检查)

{"d":[{"__type":"Mywebservice+SampleClass","TaskId":"1","TaskName":"task1"},{"__type":"Mywebservice+SampleClass","TaskId":"2","TaskName":"task2"},{"__type":"Mywebservice+SampleClass","TaskId":"3","TaskName":"task3"},{"__type":"Mywebservice+SampleClass","TaskId":"4","TaskName":"task4"}]}

我在浏览器的任何地方都没有得到任何异常。一切看起来都很完美,但我仍然不明白为什么它没有将网格与数据绑定(bind)。它只显示一个带有列标题和分页工具栏的空网格。

谁能告诉我这里缺少什么?

最佳答案

查看您的商店,我发现您定义了代理/阅读器 - 但您的模型 配置在错误的对象上。

model 应该在商店里——而不是读者:

    var storeData = new Ext.data.Store(
{
autoLoad: true,
model: 'myModel',
proxy: new Ext.ux.AspWebAjaxProxy({
url: '/ProjectManagement/Mywebservice.asmx/GetTasks',
actionMethods: {
read: 'POST'
},
reader: {
type: 'json',
root: 'd'
},
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
});

关于c# - 如何使用 Asp.net Websrvice 绑定(bind) ExtJS 网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20139419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com