Results 1 to 3 of 3

Thread: MVC and autocomplete textbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    MVC and autocomplete textbox

    i m working autocomplete in textbox .for examle if i type e in textbox it should displayed all username start with 'e' from datatbase i write following code ..but its not working fine ..

    @model EMS1.Models.UsersViewModel

    @{
    ViewBag.Title = "searchuser";
    }
    @Styles.Render("~/Content/themes/base/css")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @*<script type="text/javascript" language="javascript">
    $(document).ready(function () {
    $('#txt_search').autocomplete({
    source: '@Url.Action("Autocomplete")'
    });
    })
    </script>*@

    <script type="text/javascript" language="javascript">
    $(document).ready(function () {
    $("#txt_search").autocomplete({
    source: function (request, response) {
    $.ajax({
    datatype: 'json',
    url: '@Url.Action("Autocomplete")',
    data: "{ 'term': " + "'" + $("#txt_search").val() + "'" + "}",
    type: 'POST',
    success: function (data) {
    response($.map(data, function (item) {
    return { label: item.UserName, value: item.UserName };
    }))
    },
    error: function (error) {
    alert("error" + error.tostring());
    }

    })
    },
    messages: {
    noResults: "", results: ""
    }


    });
    })
    </script>
    <h2>searchuser</h2>
    @using (Html.BeginForm()) {
    <div>
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
    <legend>Login Here</legend>
    <table border="1">
    <tr>
    <td>search User</td>
    <td> @Html.TextBox("txt_search")</td>

    </tr>

    </table>
    <p>
    <input type="submit" value="Search user" />
    </p>
    </fieldset>
    </div>
    }


    public string Autocomplete(string term)
    {


    DataSet ds = new DataSet();
    ds = ts.SelectQueryDS("select username from tbl_user where username like " + "'" + term + "%'");

    return GetJson(ds.Tables[0]);

    }

    public string GetJson(DataTable dt)
    {
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
    Dictionary<string, object> row = null;

    foreach (DataRow dr in dt.Rows)
    {
    row = new Dictionary<string, object>();
    foreach (DataColumn col in dt.Columns)
    {
    row.Add(col.ColumnName.Trim(), dr[col]);
    }
    rows.Add(row);
    }
    string s = serializer.Serialize(rows);
    return serializer.Serialize(rows);

    }
    There is no achievement without goals

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: MVC and autocomplete textbox

    i m working autocomplete in textbox .for examle if i type e in textbox it should displayed all username start with 'e' from datatbase i write following code ..but its not working fine ..
    Did you mean it's not returning records from the database?

    KGC
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2007
    Location
    Karachi
    Posts
    551

    Re: MVC and autocomplete textbox

    yes not showing
    There is no achievement without goals

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width