Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 剑道网格和一行JSON数据和一行数据_Asp.net Mvc_Json_Kendo Ui_Kendo Grid - Fatal编程技术网

Asp.net mvc 剑道网格和一行JSON数据和一行数据

Asp.net mvc 剑道网格和一行JSON数据和一行数据,asp.net-mvc,json,kendo-ui,kendo-grid,Asp.net Mvc,Json,Kendo Ui,Kendo Grid,我将JSON结果输入我的剑道网格;好像当我有一行数据时,网格就不能工作了 以下工作: <DocumentElement> <ResultXml> <NoData>1</NoData> </ResultXml> <ResultXml> <NoData>2</NoData> </ResultXml> </DocumentEl

我将JSON结果输入我的剑道网格;好像当我有一行数据时,网格就不能工作了

以下工作:

<DocumentElement>
    <ResultXml>
        <NoData>1</NoData>
    </ResultXml>
    <ResultXml>
        <NoData>2</NoData>
    </ResultXml>
</DocumentElement>";
下面的工作很有效,但如果不需要,我希望避免发送空字段

{"DocumentElement":{"ResultXml":[{"NoData":null},{"NoData":"Nothing to display"}]}}
编辑结束

这是我的剑道格网:

$("#grid").kendoGrid({
    sortable: true,
    groupable: true,
    scrollable: true,
    height: "600px",
    pageable: { pageSizes: 9 },
    dataSource:
    {
        transport:
        {
            read: function (options) {
                $.ajax("/Controller/Action?param=" + paramVal, 
                 success: function (result) {
                    var jResult = $.parseJSON(result);
                    options.success(jResult.DocumentElement.ResultXml);
                });
            }
        }
    },
});

我想我可以在将json字符串发送回客户端时对其进行硬编码;我希望有一个xml版本,说我有空的数据集;但我想这就行了,除非有人能提出更好的建议

if (string.IsNullOrEmpty(xmlResult))
{
    //No data: 
    jsonData = "{\"DocumentElement\":{\"ResultXml\":[{\"NoData\":\"Nothing to display\"}]}}";
}
else
{
    //Turn xml data to Json:
    var doc = new XmlDocument();
    doc.LoadXml(xmlResult);
    jsonData = JsonConvert.SerializeXmlNode(doc);
}

我相信字段名不需要双引号。Ex/name\value对如{name:“Bob Mazzo”}

您确定控制器中有两个定义(一个用于查看,另一个用于检索数据),并且视图调用另一个定义,如下所示

控制器:

//!!! This is for calling "CustomAjaxBinding" view
public ActionResult CustomAjaxBinding()
{
    return View();
}

//!!! This is for retrieving data called from "CustomAjaxBinding" View
public ActionResult CustomAjaxBinding_Read([DataSourceRequest] DataSourceRequest request)
{
    var dataContext = new SampleEntities();

    //Convert to view model to avoid JSON serialization problems due to circular references.
    IQueryable<OrderViewModel> result = dataContext.Orders.Select(o => new OrderViewModel
    {
        OrderID = o.OrderID,
        ShipCity = o.ShipCity,
        ShipCountry = o.ShipCountry,
        ShipName = o.ShipName
    });
return Json(result, JsonRequestBehavior.AllowGet);      
}
/!!!这用于调用“CustomAjaxBinding”视图
public ActionResult CustomAjaxBinding()
{
返回视图();
}
//!!! 这用于检索从“CustomAjaxBinding”视图调用的数据
public ActionResult CustomAjaxBinding_Read([DataSourceRequest]DataSourceRequest请求)
{
var dataContext=新的SampleEntities();
//转换为视图模型以避免由于循环引用而导致的JSON序列化问题。
IQueryable结果=dataContext.Orders.Select(o=>new OrderViewModel
{
OrderID=o.OrderID,
ShipCity=o.ShipCity,
ShipCountry=o.ShipCountry,
ShipName=o.ShipName
});
返回Json(结果,JsonRequestBehavior.AllowGet);
}


视图(名称为“CustomAjaxBinding”)

@(Html.Kendo().Grid())
.名称(“网格”)
.列(列=>{
columns.Bound(o=>o.OrderID).Groupable(false);
columns.Bound(o=>o.ShipCity);
columns.Bound(o=>o.ShipCountry);
columns.Bound(o=>o.ShipName);
})
.Pageable()
.Sortable()
.可过滤()
.Scrollable()
.Groupable()
.DataSource(DataSource=>DataSource
.Ajax()
//!!!调用“CustomAjaxBinding_Read”而不是CustomAjaxBinding
.Read(Read=>Read.Action(“CustomAjaxBinding\u Read”,“Grid”))
)
)


关于…

伙计,你确定这些废话是JSON吗?您应该将其视为XML,您对gohaha很好,谢谢!这是一个正确的观点:)我忘了包括将xml转换为json的部分;我将把问题再往下编辑。不过还是要感谢:)我认为问题在于将“field:val”对视为对象而不是数组;当我有两个“field:val”的实例时,JsonConvertor将其转换为一个数组;但如果只有一个实例,它就变成了一个对象;所以我不得不在Kendo版本的JSON数据源和Newton的转换方式之间进行斗争;JsonConvertor没有创建数组是正确的;如果字段不在JSON数组中,Kendo不显示字段是错误的
if (string.IsNullOrEmpty(xmlResult))
{
    //No data: 
    jsonData = "{\"DocumentElement\":{\"ResultXml\":[{\"NoData\":\"Nothing to display\"}]}}";
}
else
{
    //Turn xml data to Json:
    var doc = new XmlDocument();
    doc.LoadXml(xmlResult);
    jsonData = JsonConvert.SerializeXmlNode(doc);
}
//!!! This is for calling "CustomAjaxBinding" view
public ActionResult CustomAjaxBinding()
{
    return View();
}

//!!! This is for retrieving data called from "CustomAjaxBinding" View
public ActionResult CustomAjaxBinding_Read([DataSourceRequest] DataSourceRequest request)
{
    var dataContext = new SampleEntities();

    //Convert to view model to avoid JSON serialization problems due to circular references.
    IQueryable<OrderViewModel> result = dataContext.Orders.Select(o => new OrderViewModel
    {
        OrderID = o.OrderID,
        ShipCity = o.ShipCity,
        ShipCountry = o.ShipCountry,
        ShipName = o.ShipName
    });
return Json(result, JsonRequestBehavior.AllowGet);      
}
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.Order>()
    .Name("Grid")
    .Columns(columns => {
        columns.Bound(o => o.OrderID).Groupable(false);
        columns.Bound(o => o.ShipCity);
        columns.Bound(o => o.ShipCountry);
        columns.Bound(o => o.ShipName);
    })
    .Pageable()
    .Sortable()
    .Filterable()
    .Scrollable()
    .Groupable()
    .DataSource(dataSource => dataSource
        .Ajax()

        //!!! Call "CustomAjaxBinding_Read" not CustomAjaxBinding
        .Read(read => read.Action("CustomAjaxBinding_Read", "Grid"))
    )
)