Typeahead.js for MVC 5 Models

前端 未结 1 2028
故里飘歌
故里飘歌 2021-01-25 03:29

Ive just imlemented typeahead functionality using thw Typeahead.js for MVC 5 Models wrapper

http://timdwilson.github.io/typeahead-mvc-model/

it all works ok but

相关标签:
1条回答
  • 2021-01-25 03:56

    The key is to use the linq "Take"

    This works: 1) Create an action in your controller and set the RouteConfig to start this action

    public class HomeController : Controller
    {
        public ActionResult Index20()
        {
            MyViewModel m = new MyViewModel();
            return View(m);
        }
    

    Create a view without any type of master page Add this view model:

    public class MyViewModel
    {
        public string SourceCaseNumber { get; set; }
    }
    

    Go to Manage Nuget Packages or PM Console and add to MVC 5 project - Typeahead.js for MVC 5 Models by Tim Wilson Change the namespace for the added HtmlHelpers.cs to System.Web.Mvc.Html and rebuild Add this class:

    public class CasesNorm
    {
        public string SCN { get; set; }
    }
    

    Add these methods to your controller:

    private List<Autocomplete> _AutocompleteSourceCaseNumber(string query)
        {
            List<Autocomplete> sourceCaseNumbers = new List<Autocomplete>();
            try
            {
                //You will goto your Database for CasesNorm, but if will doit shorthand here
    
                //var results = db.CasesNorms.Where(p => p.SourceCaseNumber.Contains(query)).
                //    GroupBy(item => new { SCN = item.SourceCaseNumber }).
                //    Select(group => new { SCN = group.Key.SCN }).
                //    OrderBy(item => item.SCN).
                //    Take(10).ToList();   //take 10 is important
    
                CasesNorm c1 = new CasesNorm { SCN = "11111111"};
                CasesNorm c2 = new CasesNorm { SCN = "22222222"};
                IList<CasesNorm> aList = new List<CasesNorm>();
                aList.Add(c1);
                aList.Add(c2);
                var results = aList;
    
                foreach (var r in results)
                {
                    // create objects
                    Autocomplete sourceCaseNumber = new Autocomplete();
    
                    sourceCaseNumber.Name = string.Format("{0}", r.SCN);
                    sourceCaseNumber.Id = Int32.Parse(r.SCN);
                    sourceCaseNumbers.Add(sourceCaseNumber);
                }
            }
            catch (EntityCommandExecutionException eceex)
            {
                if (eceex.InnerException != null)
                {
                    throw eceex.InnerException;
                }
                throw;
            }
            catch
            {
                throw;
            }
            return sourceCaseNumbers;
        }
    
        public ActionResult AutocompleteSourceCaseNumber(string query)
        {
            return Json(_AutocompleteSourceCaseNumber(query), JsonRequestBehavior.AllowGet);
        }
    
         throw;
        }
        catch
        {
            throw;
        }
        return sourceCaseNumbers;
    }
    
    public ActionResult AutocompleteSourceCaseNumber(string query)
    {
        return Json(_AutocompleteSourceCaseNumber(query), JsonRequestBehavior.AllowGet);
    }
    

    credit goes to http://timdwilson.github.io/typeahead-mvc-model/

    0 讨论(0)
提交回复
热议问题