Search code examples
c#asp.netajax

Remove extra object from c# list<object> return json response from asp.net Webform


I'm newbie in asp.net webform. I have so many problems that's almost I got solution from StackOverflow. But Now I'm facing an issue and can't get any desired solution, So, I'm writing this question. I want a textbox to autocomplete with JQuery autocomplete call. I have two textboxes, one is for autocomplete suggestion list, another is getting selected item Id. I don't know how modern website do this. But fortunately, I got a hints by googling it.

Here I have create a codebehind [WebMethod] attribute Method:

// Class for Custom datatype object
public class MyData
{
    public int Id { get; set; }
    public string Value { get; set; }
}

// Webmethod for call from ajax
[WebMethod]
public static MyData GetDataList()
{
    int id = 123;
    string value = "My Name";
    return new MyData { Id = id, Value = value };
}

And here is my JQuery function.

$(function () {
    $("#autocomplete").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: 'POST',
                url: 'reissue_ticket.aspx/GetDataList',
                data: '{ "term": "' + request.term + '" }',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {
                    response(data.d);
                },
                error: function () {
                    console.log('Error fetching items.');
                }
            });
        },
        minLength: 1,
        select: function (event, ui) {
            // Set the selected value and id into textboxes
            $('#selectedValue').val(ui.item.value);
            $('#selectedId').val(ui.item.id);
        }
    });
});

It's work fine but problem is I got 3 object response. like:

json respose text

{
    "d": {
        "__type": "eLedger.com.Pages.Services.reissue_ticket+MyData",
        "Id": 123,
        "Value": "My Name"
    }
}

I don't want"__type": "eLedger.com.Pages.Services.reissue_ticket+MyData". I tryed in c# like Json Serialized but nothing works.

Please someone help me to get this problem solve or any better idea with webform.

Thanks in advance for reading my problem at least.

I want the response like {"Id":1, "Value": "My Name"} only.


Solution

  • Thanks everyone for the reading, comments, and answer. I solve this problem in different ways with anonymous object method that works file with few efforts. here is my solution:

    public static object[] Airlines(string prefix)
    {
        string query = string.Format("SELECT AirlineID, AirlineName FROM airlines WHERE AirlineName LIKE CONCAT('%', @name, '%') OR AirlineCode= @code ");
        MySqlParameter param1 = new MySqlParameter("@name", prefix.Trim());
        MySqlParameter param2 = new MySqlParameter("@code", prefix.Trim());
        DatabaseManager dbManager = new DatabaseManager();
        DataTable dt = dbManager.ExecuteQuery(query, param1, param2);
        var objectArray = dt.AsEnumerable()
            .Select(row => new
            {
                Id = row["AirlineID"],
                Name = row["AirlineName"],
            })
           .ToArray();
        return objectArray;
    }