i have a issue , my problem is it i can't use my object form my model using EF , i need this method for a autocomplete search; this is my error message :
"The System.NotSupportedException exception occurred HResult = 0x80131515 Message = The entity type or complex type 'PizzaTn.Context.ViewModels' cannot be built in a LINQ to Entities query. Source = Procedure call tree:
public JsonResult GetSearchValue(string search)
{
PizzaTnContext db = new PizzaTnContext();
List<VilleModels> allsearch = new List<VilleModels>();
allsearch = db.Villes.Where(x => x.VilleName.Contains(search)).Select(x => new VilleModels
{
IdVille = x.IdVille,
VilleName = x.VilleName
}).ToList();
return new JsonResult { Data = allsearch, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
this my HTML
<script>
$("#searchInput").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("GetSearchValue", "Home")',
datatype: "json",
data: {
search: $("#searchInput").val()
},
success: function (data) {
response($.map(data, function (item) {
return { label: item.VilleName, value: item.VilleName };
}
)
);
},
error: function (xhr, status, error) {
alert("error");
}
});
}
});
</script>
the solution is to add an argumant .AsEnumerable() .. thanks George:
db.Villes.Where(x => x.VilleName.Contains(search)).AsEnumerable().Select(x => new VilleModels { IdVille = x.IdVille, VilleName = x.VilleName }).ToList();
User contributions licensed under CC BY-SA 3.0