get data from database using Json for search method

0

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>
c#
jquery
.net
linq-to-entities
asp.net-mvc-5
asked on Stack Overflow Sep 24, 2017 by Ouss Ama

1 Answer

0

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();
answered on Stack Overflow Sep 26, 2017 by Ouss Ama

User contributions licensed under CC BY-SA 3.0