none
Auto complete text field asp.net mvc wont work RRS feed

  • Question

  • This is my controller class
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using AutoC.Models;
    
    namespace AutoC.Controllers
    {
        public class AutoController : Controller
        {
            AutoJEntities db = new AutoJEntities();
    
            // GET: NewA
    
            [HttpGet]
            public ActionResult Index()
            {
                return View();
            }
    
    
            [HttpPost]
            public JsonResult Indexv(string Prefix)
            {
                var country = (from x in db.Students where x.Name.Contains(Prefix) select new { x.Name });
                return Json(country, JsonRequestBehavior.AllowGet);
            }
        }
    }using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using AutoC.Models;
    
    namespace AutoC.Controllers
    {
        public class AutoController : Controller
        {
            AutoJEntities db = new AutoJEntities();
    
            // GET: NewA
    
            [HttpGet]
            public ActionResult Index()
            {
                return View();
            }
    
    
            [HttpPost]
            public JsonResult Indexv(string Prefix)
            {
                var country = (from x in db.Students where x.Name.Contains(Prefix) select new { x.Name });
                return Json(country, JsonRequestBehavior.AllowGet);
            }
        }
    }
    and this is View page
    @model AutoC.Models.Student
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    
    
    <link href="~/Content/jquery-ui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.10.2.js"></script>
    <script src="~/Scripts/jquery-ui.js"></script>
    <script type="text/javascript">
    
        $(document).ready(function () {
            $("#Name").autocomplete({
                source: function (request, response) {
    
                    $.ajax({
                        url: "/Home/Indexv",
                        type: "POST",
                        dataType: "json",
                        data: { prefix: request.term },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return { label: item.Name, value: item.Name };
                            }
                            ))
                        }
                    })
    
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert("errorThrown" + errorThrown);
                }
            })
        })
    </script>
    
    
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
    
            <hr />
    
            <div class="form-group">
    
                <div class="col-md-12">
                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
    
                </div>
            </div>
    
        </div>
    }
    
    
    
    
    
    expected output is to autocomplete text box with database data, but it not working not even it returns error , i cant see where the error is.
    • Moved by CoolDadTx Monday, February 12, 2018 2:48 PM ASP.NET related
    Sunday, February 11, 2018 9:00 AM

All replies

  • Hi PathumLakshan,

    Thank you for posting here.

    For your question is more related to MVC, please post a new thread in MVC forum for suitable support.

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 12, 2018 7:21 AM