Click here to Skip to main content
15,036,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys,
i am trying to develop autocomplete using bootstrap
that is my code:

[View]

@model Medica.ViewModel.CompanyOrderItemModel

<script src="~/Scripts/typehead.js"></script>

@{
//ViewBag.Title = Medica.Resources.Resources.AddOrderItem;
ViewBag.Header = Medica.Resources.Resources.AddOrderItem;
Layout = null;
}

<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">@Medica.Resources.Resources.AddItem</h4>
</div>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<div class="modal-body">
@Html.HiddenFor(model => model.Item.CompanyOrderID)
<div class="editor-label">
@Medica.Resources.Resources.Item
</div>
<div class="editor-field">
@Html.HiddenFor(model => model.Id)
<input type="text" name="names" value="" id="typeahead" data-provide="typeahead" />
@Html.ValidationMessageFor(model => model.Item.ItemID)
</div>

<div class="editor-label">
@Medica.Resources.Resources.RequiredQuantity
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Item.OrderItemRequiredQuantity)
@Html.ValidationMessageFor(model => model.Item.OrderItemRequiredQuantity)

</div>
</div>
}

<div class="modal-footer">
<button class="btn" data-dismiss="modal">Cancel</button>
<input class="btn btn-primary" type="submit" value="@Medica.Resources.Resources.Add" />
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

<script type="text/javascript">
$(function () {
$('#typeahead').typeahead({
source: function (term, process) {
var url = '@Url.Action("GetNames", "ItemApi")';

return $.getJSON(url, { term: term }, function (data) {
return process(data);
});
}
});
})
</script>


[controller]


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
//using System.Web.Http;
using Medica.BusinessLayer;
using Medica.Models;
using System.Web.Mvc;
 

namespace Medica.Controllers
{
    public class ItemApiController : Controller
    {
        MedicaEntities dbMedica = new MedicaEntities();
        

        [HttpGet]
        public JsonResult GetNames(string term)
        {
            var names = (from u in dbMedica.Items
                         where u.LatinName.Contains(term)
                         select u).ToArray();
            var results = names.Select(u => u.LatinName);

            return new JsonResult()
            {
                Data = results,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
       
    }
}


the problem is when i type anything the function getnames doesn't fired can any one help my to solve this problem ?
thanks in advanced .
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900