目前,javascript应该收到数据,但它现在显示在网格中。
以下是我的* .cshtml文件中的代码,用于显示给定查询的结果:
<div class="col-md-6 fill-height">
<div class="row btn-row">
<div class="sxs">
<button id="run-test-rule-btn" title="Test Rule">
</button>
<span class="button-caption">Test Rule</span>
</div>
</div>
<div class="row">
<textarea id="rule-sql" name="Rule Sql" style=""></textarea>
</div>
<div class="row fill-height">
<div id="query-results-grid">
</div>
</div>
</div>
以下是我的* .js文件中的一些代码:
var IDS = {
queryResultsGrid: "#query-results-grid",
...
runTestRuleBtn: "#run-test-rule-btn",
};
var Grids = {
TestRuleResultsGrid: null,
...
};
var PageState = {
...
AddingRule: false,
TestRuleResult: null
};
function initQueryResultsGrid() {
$(IDS.queryResultsGrid).kendoGrid({
selectable: true,
scrolable: true,
sortable: false,
columns: [
{ field: "UWI", title: "UWI", width: "100%", attributes: { tabindex: "1" } }
],
change: function() {
var selectedDataItem = this.dataItem(this.select());
if (PageState.Selected.TestRuleResult !== selectedDataItem.TestRuleResult) {
PageState.Selected.TestRuleResult = selectedDataItem.TestRuleResult;
testRuleResultsSelectionChanged();
}
},
editable: false
});
Grids.TestRuleResultsGrid = $(IDS.queryResultsGrid).data('kendoGrid');
}
function execTestRule() {
$.ajax({
type: 'POST',
url: "ExecuteTestRule",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({
ruleSql : PageState.SqlEditor.RuleSql.getValue(),
parameters: PageState.RuleParameters
}),
success: function (matchedUwiList) {
PageState.TestRuleResult = matchedUwiList;
var dataSource = new kendo.data.DataSource({
data: matchedUwiList
});
Grids.TestRuleResultsGrid.setDataSource(dataSource);
PageState.Selected.ChildUwi = null;
updateButtonStates();
}
});
}
以下是我的控制器中的代码:
public ActionResult ExecuteTestRule(string ruleSql, List<PdsMatchRuleParam> parameters = null)
{
return Json(new MatchDataSource().ExecuteTestRule(ruleSql, parameters), JsonRequestBehavior.AllowGet);
}
以下是我模型中的代码:
public List<string> ExecuteTestRule(string ruleRawSql, List<MatchRuleParam> parameters)
{
var da = new DataAccess();
var ruleSql = ruleRawSql.Replace(@"{KEY}", "#*$pkey");
var dbParameters = new List<DataAccess.DbParameter>();
dbParameters.Add(new DataAccess.DbParameter("pkey", DbType.AnsiString, 4000,
parameters[4].DefaultValue));
var dt = da.Select(ruleSql, dbParameters.ToArray());
var uwis = (from DataRow r in dt.Rows
select r["UWI"].ToString()).ToList();
return uwis;
}
以下是dataSource Grids.TestRuleResultsGrid.setDataSource(dataSource);
中存在的数据,因此我知道它从数据库中获取了正确的数据:
matchedUwiList
["10115348", "10115348-001", "10117434-000"]
我错过了什么? TIA。
答案 0 :(得分:1)
这需要处理很多,但我的第一直觉是更改您的ExecuteTestRule
以返回包含UWI
作为字段名称的json结果
public ActionResult ExecuteTestRule(string ruleSql, List<PdsMatchRuleParam> parameters = null)
{
return Json(new MatchDataSource().ExecuteTestRule(ruleSql, parameters)
.Select(a => new {UWI = a}), JsonRequestBehavior.AllowGet);
}
答案 1 :(得分:0)
@ JamieD77有正确的想法 - 这就是我最终做的 - 添加一个匿名类型来为JSON对象提供一些结构,尽管@ JamieD77的答案更好,因为他保留了匿名对象在控制器中,我认为它是一个更好的地方:
public object ExecuteTestRule(string ruleRawSql, List<PdsMatchRuleParam> parameters)
{
var da = new DataAccess();
var ruleSql = ruleRawSql.Replace(@"{KEY}", "#*$pKey");
var dbParameters = new List<DataAccess.DbParameter>();
dbParameters.Add(new DataAccess.DbParameter("pKey", DbType.AnsiString, 4000,
parameters[4].DefaultValue));
var dt = da.Select(ruleSql, dbParameters.ToArray());
var uwis = (from r in dt.AsEnumerable()
select new
{
UWI = r.ToString()
}).ToList();
return uwis;
}