0
{"__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"},

this is the webmethod that I return by json, but the problem is, asp.net return value from base class that I inherit from, how to solve this? although is working, but the data are wasted as I don't wish to return the value, any idea?

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();
}
5
  • do you want to return only a part of data of MenuFunctionModel? Commented Jun 18, 2013 at 9:35
  • yes, I just wanna to return the part that inside the List, but it seen like it return everything, due to to List<MenuFunctionModel>? Commented Jun 18, 2013 at 9:37
  • This is due to the return type (List<CountryModel>) you have used in webmethod. you can use another class that contains only required number of fields, or use anynomous type Commented Jun 18, 2013 at 9:48
  • any example link that I can refer to? or I change every base class public variable to private, but this will had more change impact to other classes as I plan to use all the variable as global Commented Jun 18, 2013 at 9:49
  • Should be the class that inherit to the base model that cause the problem, any solution? Commented Jun 18, 2013 at 10:00

1 Answer 1

1

You can return anynomous types from your WebMethod as:

[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
}

For more references, Click here

Sign up to request clarification or add additional context in comments.

1 Comment

always welcomed!. We all are thankful to this great community community for sure!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.