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:
{
"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.
2
Answers
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:
Your public function is already "automatic" typed as "MyData"
And yes, the built in serializer always did include the object type, and it always returns the data in the .d part of the object returned.
Also, your .ajax call is passing data, and that means the function signature should match (you are passing "term").
So, say this method:
So, on the client side, then this:
result:
Note close, we have to prefix the return values with ".d".
.net been this way for 20 years, and it as a security measure always returns the payload in ".d".
And yes, the object type is always returned, and again in 99% of cases, we don’t care.
However, there are several approaches if you want to control what is returned. You can set the web method function as a string and then serialize the object server side, and then client side serialize again. (I don’t particular like doing this, since you doing a double serialize).
If you really don’t want that extra information, and don’t want the object type included? And you don’t want the .d payload?
Then simply serialize the object yourself, and do NOT strong type the web method. In fact, mark the method as void, and return whatever you want.
Hence, say this:
And server side, we do this:
And now client side, we have this:
Output:
Note that I introduced JSON.stringify, since I don’t like the messy concatenation, and in above, stringify can be used in all of the above examples.
So, we might have say:
Then data becomes this:
And the server-side signature has to match, and thus this:
Note that prior to .net 4.7.2, it is recommended that you use Newton Soft’s serializer.
For current 4.8, then you can in most cases use the built in serializer.
Hence:
So, you can mark the web method as void, and return whatever you want, and above will remove the ".d", and will remove the passing of the object type.
However, in most cases, and even using autocomplete from jQuery.UI, then in client-side code, you can just include the .d for your client-side code, and autocomplete can and will work just fine.
Edit: AutoComplete example
As I pointed out, the fact of the object type, and that of the return value being inside of ".d"?
It really does not matter.
Hence say this markup:
And the web method behind is this:
And the result is this:
So, the fact of having to use ".d", and the fact that the object type was included did not matter. Now, I suppose one could perhaps use my above example in which we serialize the List object, and send that to jQuery autocomplete, but it not really much of an issue either way.