将EnumerableRowCollection序列化为JSONResult [英] Serializing an EnumerableRowCollection into a JSONResult

查看:84
本文介绍了将EnumerableRowCollection序列化为JSONResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下应该返回JSONResult的方法,因此我可以在具有javascript的AJAX方法中使用它,并为文本框加载自动完成建议.每当特定的下拉列表更改时,我都会加载此文件.

I have the following method which is supposed to return a JSONResult so I can use it in an AJAX method with javascript and load autocomplete suggestions for a textbox. I will load this everytime a particular dropdown list changes.

[AcceptVerbs(HttpVerbs.Post), Authorize]
    private JsonResult GetSchemaNodeValues(string SchemaNodeId)
    {
        var query = @"Select ld.""Value"" 
        From ""LookupData"" ld, ""SchemaNode"" sn
        Where sn.""LookupTypeId"" = ld.""LookupTypeId""
        And sn.""SchemaNodeId"" = '{0}'";

        DataSet data = new DataSet();


            data = ServiceManager.GenericService.ExecuteQuery(String.Format(query, SchemaNodeId)).Data;

        var res = data.Tables[0].AsEnumerable().Select(dr => new
        {
            Value = dr["Value"].ToString()
        });

        return JsonConvert.SerializeObject(res);

    }

return JsonConvert.SerializeObject(res);

Error   106 Cannot implicitly convert type 'string' to 'System.Web.Mvc.JsonResult'  

有什么办法可以克服这个问题?

Is there any way to get past this?

在此之前,我尝试使用System.Web.mvc.Controller.Json(res);从对象返回JSONResult.

Before this I tried using System.Web.mvc.Controller.Json(res); which returns a JSONResult from an object.

但是我不能使用它,因为我的类是PageDialog而不是Controller,因此它无法访问Controller的受保护内部方法,例如JSon().我得到的错误是JSon()由于其保护级别而无法访问. Controller类已锁定,我无法将其公开或创建变通办法,因此我使用JsonConvert.SerializeObject(res);更改了方法.

But I couldn't use it because my class is a PageDialog not a Controller so it doesn't have access to Controller's protected internal methods like JSon(). The error I got was that JSon() is inaccessible due to its protection level. The Controller class was locked and I can't make it public or create a workaround so I changed the approach using JsonConvert.SerializeObject(res);

任何建议都将受到欢迎.

Any suggestions would be extremely welcome.

推荐答案

private dynamic GetSchemaNodeValues(string SchemaNodeId)
{
    ...
    return data.Tables[0].AsEnumerable().Select(dr => new
    {
        Value = dr["Value"].ToString()
    });
}

private string GetSchemaNodeValues(string SchemaNodeId)
{
    ...
    var result = data.Tables[0].AsEnumerable().Select(dr => new
    {
        Value = dr["Value"].ToString()
    });
    return JsonConvert.SerializeObject(result);
}

这篇关于将EnumerableRowCollection序列化为JSONResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆