Passing list of items from Controller/Model to a variable in javascript - autocomplete
Posted
by
newbie_developer
on Stack Overflow
See other posts from Stack Overflow
or by newbie_developer
Published on 2012-10-26T10:52:31Z
Indexed on
2012/10/26
11:00 UTC
Read the original article
Hit count: 130
I've a method in a NamesModel which fetches all the names and returns a list of names:
public static List<NamesModel> GetAllNames()
{
List<NamesModel> names = new List<NamesModel>();
//
// code to fetch records
//
return names;
}
In my controller:
public ActionResult Index()
{
NamesModel model = new NamesModel();
model.GetAllNames();
return View(model);
}
In the view, I've got a textbox:
@Html.TextBox("search-name")
Now in my javascript, I want to fetch all names into a variable either from a model (from method) or from controller, for example:
<script type="text/javascript">
$(function () {
var names = ...........
$(document).ready(function () {
$('#search-name').autocomplete({
source: names
});
});
});
</script>
If I use hardcoding then it works but I want to use the names stored in the db. Is it possible?
hardcoding example:
var names = ["abc", "xyz"];
© Stack Overflow or respective owner