如何从Web方法返回对象 [英] How to return object from Web Method

查看:67
本文介绍了如何从Web方法返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在dhtmlx甘特图中显示任务进度。我正在使用asp.net web表单我希望以下列格式返回数据

I want to display Task Progress in dhtmlx gantt chart. I am using asp.net web form I want to return data in the following format

var tasks = {
        data: [
            { id: 1, text: "Project #2", start_date: "01-04-2013", duration: 18, order: 10,
                progress: 0.4, open: true
            },
            { id: 2, text: "Task #1", start_date: "02-04-2013", duration: 8, order: 10,
                progress: 0.6, parent: 1
            }
        ]
            ,
        links: [
        { id: 1, source: 1, target: 2, type: "1" },
        { id: 2, source: 2, target: 3, type: "0" },
    ]
    };





我试图返回它来自web方法我创建了以下web方法...



I was trying to return it from web method I have created following web method...

[System.Web.Services.WebMethod]
public static string GetProjectTask(ImproperEvents item)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    Project pro = new Project { ProjectId = int.Parse(item.ProjectId) };

    DataTable dt = pro.GetProjectTask();
    List<GanntData> taskList = new List<GanntData>();
    List<GanttLink> linkList = new List<GanttLink>();

    //string data = "data:";
    //string link = "link: [";
    foreach (DataRow row in dt.Rows)
    {
        taskList.Add(new GanntData
        {
            id = int.Parse(row["TaskId"].ToString())
            ,text = row["TaskName"].ToString()
            ,start_date = string.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(row["StartDate"].ToString()))
            ,duration = int.Parse(row["Duration"].ToString())
            ,order = 0
            ,progress = int.Parse(row["Progress"].ToString())
            ,parent = row["ParentId"] == DBNull.Value ? 0 : int.Parse(row["ParentId"].ToString())
        });            
    }

    return serializer.Serialize(taskList);


    //return data;
}



但是无法获得理想的结果。我哪里错了?有人知道如何解决它


But not able to get desired result. where am i gone wrong ? Anybody knows how to can I solve it

推荐答案

示例代码:

sample code :
RootObject ob = new RootObject();
ob.data = new List<datum>() { new Datum() { id = 1, text = "aa", start_date = "01-04-2013", progress = 18, order = 10, open = true } };
ob.links = new List<link>() { new Link() { id = 1, source = 1, target = 2, type = "1" } };
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return serializer.Serialize(ob);



您需要如下课程


you need class like below

public class Datum
    {
        public int id { get; set; }
        public string text { get; set; }
        public string start_date { get; set; }
        public int duration { get; set; }
        public int order { get; set; }
        public double progress { get; set; }
        public bool open { get; set; }
        public int? parent { get; set; }
    }

    public class Link
    {
        public int id { get; set; }
        public int source { get; set; }
        public int target { get; set; }
        public string type { get; set; }
    }

    public class RootObject
    {
        public List<Datum> data { get; set; }
        public List<Link> links { get; set; }
    }
}


这篇关于如何从Web方法返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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