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 %>");
}
});
GetItems
.data
is just one long string, and with that one long string, you're only creating one <option> with that long string as its value. HTML rendering then turns the newlines into spaces. – millimoose Oct 27 '11 at 19:34