-
May 17th, 2015, 06:35 AM
#1
Thread Starter
Fanatic Member
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
-
May 25th, 2015, 05:19 AM
#2
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
-
Jun 10th, 2015, 09:46 PM
#3
Thread Starter
Fanatic Member
Re: MVC and autocomplete textbox
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|