I tried to protect a string with the JavaScriptStringEncode library and it is showing the following error:
Cannot convert from 'System.Collections.Generic.IEnumerable<<anonymous type: string Nome, System.Guid Id>>' to 'string'
Controller:
public JsonResult GetBarcosNaoVinculados(string usuarioId)
{
var linkedboatds= this._boatsAppService.GetUnboundBoats(usuarioId)
.Select(boats=> new { boats.Name, boats.Id });
return Json(HttpUtility.JavaScriptStringEncode(linkedboatds),
JsonRequestBehavior.AllowGet);
}
To solve this, I converted it to string, using toString (), but when I get to the jQuery function, I get the following error:
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in System.Linq.Enumerable+WhereSelectListIterator
2[BR.Rve.Application.ViewModels.Embarcacao.BarcoViewModel,\u003c\u003ef__AnonymousType112[System.String,System.Guid]]
jQuery script:
function searchForAvailableBoats {
$ddlBarcosDisponiveis.find("option").remove();
function callBack(data, status) {
if (data && data.length) {
//Add data to dropdown
var options = ""
$.each(data, function (index, item) {
options += '<option value="' + item.Id + '">' + item.Nome + '</option>';
});
$ddlBarcosDisponiveis.append(options);
}
}
sistema.ajax.get('AdminUser/GetBarcosNaoVinculados?usuarioId=' + getUserId(), callBack);
}
How could I solve this problem in the controller or in the jQuery?
