webmethod返回值形成基类

时间:2013-06-18 09:29:36

标签: asp.net json web-services

{"__type":"CountryModel","dt":null,"ds":null,"dtRow":null,"strSQL":{"Capacity":16,"MaxCapacity":2147483647,"Length":0},"iCnt":0,"ConnType":"FP","Code":"AE","Value":"United Arab Emirates"},{"__type":"CountryModel","dt":null,"ds":null,"dtRow":null,"strSQL":{"Capacity":16,"MaxCapacity":2147483647,"Length":0},"iCnt":0,"ConnType":"FP","Code":"AF","Value":"Afghanistan"},{"__type":"CountryModel","dt":null,"ds":null,"dtRow":null,"strSQL":{"Capacity":16,"MaxCapacity":2147483647,"Length":0},"iCnt":0,"ConnType":"FP","Code":"AG","Value":"Antigua and Barbuda"},

这是我通过json返回的web方法,但问题是,asp.net从我继承的基类返回值,如何解决这个问题?虽然工作正常,但数据浪费了,因为我不想回报价值,任何想法?

BaseModel.cs

   public string ConnType="";
   public datatable dt;

CountryModel.cs:

public class CountryModel:BaseModel{
   public List<MenuFunctionModel> AssignList()
{
    var _List = new List<MenuFunctionModel>();

    foreach (DataRow dr in dt.Rows)
    {
        _List.Add(new MenuFunctionModel
        {
            Code = dr["Code"].ToString(),
            Title = dr["Title"].ToString(),
            Description = dr["Description"].ToString(),
            Location = dr["Location"].ToString() + dr["FileName"].ToString()
        });
    }
    return _List;
}
}

webservices api:

    [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<CountryModel> loadCt()
{
    CountryModel _Country = new CountryModel();
    _Country.SelectAll();
    return _Country.AssignList();
}

1 个答案:

答案 0 :(得分:1)

您可以从WebMethod返回匿名类型:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static Object loadCt()
{
    CountryModel _Country = new CountryModel();
    _Country.SelectAll();
    return _Country.AssignList().Select(m=> new{ Code=m.Code, Title =m.Title}); //select those fields only which you want to return
}

有关更多参考资料,请Click here