I am populating a SelectList w/ data that comes back from an AJAX call. However, the data is showing up as one item w/ a really long name instead of several dozen items w/ short names. I’m getting “ABC DEF GHI JKL MNO” instead of “ABC”, “DEF”, “GHI”, etc. So, my SelectList just gets one item attached w/ a long name.
If I view my returned data in a debugger, I can see the short values appearing on separate lines as desired.
I’ve tried using “html” instead of “json” for the return-type, but that had no effect.
Here is my server-side code:
private void GetItems(HttpContext context, int myID)
{
DataTable datatable = GetMyItems(myID);
foreach (DataRow myRow in datatable.Rows)
{
Response.Write(myRow["ColumnName"].ToString() + Environment.NewLine);
}
Response.End();
}
And javascript…
$.ajax({
url: "../myAjaxPage.aspx?myID=" + myID,
type: 'POST',
datatype: 'json',
success: function (data) {
$("<option>").attr("value", data).text(data).appendTo("#<%= mySelectList.ClientID %>");
}
});
You aren’t returning JSON, you are just returning a string. But, that’s fine. Just split the string and iterate the resulting Array.