Click here to Skip to main content
14,826,846 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
i am using Auto complete text box in MVC. Here the data is bind to textbox when i entered any key in textbox its populating database values. the problem is i want how to get id's and bind to one hiddenfied. 



thanks in advance.
syam k


What I have tried:

In controller:

public JsonResult Langs(string Prefix)
{
CRA_DBEntities dbname = new CRA_DBEntities();
var dbnames = new object();
//List<string> dbnames;
var dbnameswithid = new object();
dbnameswithid = dbname.ConstantDatabaseTypes.Where(x => x.DatabaseName.Contains(Prefix)).Select(y => new { id = y.ConstDatabaseTypeID, label = y.DatabaseName }).ToList();
//dbnames = dbname.ConstantDatabaseTypes.Where(x => x.DatabaseName.Contains(Prefix)).Select(y => y.DatabaseName).ToList();
//dbnames = dbname.ConstantDatabaseTypes.Where(x => x.DatabaseName.Contains(term)).Select(y => new { y.ConstDatabaseTypeID, y.DatabaseName }).ToList();
//ViewData["dbnamewithid"] = dbnameswithid;

return Json(dbnameswithid, JsonRequestBehavior.AllowGet);

}



In my JS is:
$(function () {
debugger;
var ter=$("#dbComplete").val
$('#dbComplete').autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("Langs")',
type: "POST",
datatype: "json",
data: { Prefix: request.term },
success:function(data)
{
response($.map(data, function (item) {
alert(item.Name)
}));
},
error:alert('error')

});

}
});
});
Posted
Updated 9-May-16 21:56pm

1 solution

Try something like following-
JavaScript
 success:function(data)
 {
   response($.map(data, function (item) {
   return{
     label: item.Name,
     value: item.Id
    };
  }));
},
select: function (event, ui) {
        this.value = ui.item.label;
        $(#yourHiddenFieldId).value(ui.item.value);
        event.preventDefault();
}


Please let me know in case of further queries.
:)
   

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