I do an auto search on the site my controller is as follows.

public ActionResult AutocompleteSearch(string term) { var a_suppliers = db.Ue_suppliers.Where( a => a.ShortName.Contains(term) || a.Manager.Contains(term) || a.Name.Contains(term) || a.PhoneManager.Contains(term) || a.Phone.Contains(term) ).ToList().Select( a => new { value = a.Manager } ).Distinct(); return Json(a_suppliers, JsonRequestBehavior.AllowGet); } 

That is, I transfer the manager without problems, but how do I transfer the data? Visited this a => new { value = a.Manager } to write a comma, zero reaction

I did everything according to the variant proposed below, everything is fine, but it is tied to the value parameter. Here is my view

  <input type="text" name="name" data-autocomplete-source="@Url.Action("AutocompleteSearch", "Uchet")" class="form-control" placeholder="Поиск по " /> 

But the script below

 @section scripts{ <script> $(function () { $("[data-autocomplete-source]").each(function () { var target = $(this); target.autocomplete({ source: target.attr("data-autocomplete-source") }); }); }); </script> } 

Everything works fine when you do a => new { value = a.Manager } , but if I change the controller so that autosearch works on all the fields

  public ActionResult AutocompleteSearch(string term) { var a_suppliers = db.Ue_suppliers.Where( a => a.ShortName.Contains(term) || a.Manager.Contains(term) || a.Name.Contains(term) || a.PhoneManager.Contains(term) || a.Phone.Contains(term) ).ToList().Select( a => new { value = a.Manager, a.Name, a.ShortName, a.PhoneManager, a.Phone } ).Distinct(); return Json(a_suppliers, JsonRequestBehavior.AllowGet); } 

Then he continues to search only for Managers, but already for the contents of the object.

How can I make auto search work on all records?

  • new { value = a.Manager, value2 = a.PhoneManager } so tried using a comma? - Bald
  • select in general - all columns will return - Grundy

1 answer 1

It's simple:

 a => new { manager = a.Manager, name = a.Name, phone = a.Phone, ... } 

But in general, it is better to declare the class being given and create instances of it, rather than anonymous objects.

 public class SearchDTO { public string Name {get;set;} public string Phone {get;set;} public Manager Manager {get;set;} ... } a => new SearchDTO{ Manager= a.Manager, Name = a.Name, Phone = a.Phone, ... } 
  • one
    what is better for example? - Grundy
  • one
    @Grundy a) Ability to reuse. b) The possibility of additional checks in the instance, for example, to limit the constructors. c) The ability to automatically generate an interface, say, for TypeScript (so that the receiving service can work with a typed object, and not "guess" the fields) d) Automatic documentation generation, say, for swagger, etc. - LbISS
  • one
    What can be reused if the resulting object is immediately spat in JSON? and in principle nobody knows about it on the server. And anything can be on the client and even unrelated at all - Grundy
  • @Grundy He can spit out not only from here. For example, there may be a quick search for a hint, an advanced search by parameters may be, there may be an issue somewhere else ... And in all these places it is desirable to return one object, rather than a million different ones - so as not to write a million different receivers / I twist on the client , and write one that works with a list of standardized objects. - LbISS
  • if the client only needs one / two fields out of all, it makes no sense to send all 20 - Grundy