Hello i have this code in the controller :
你好,我在控制器里有这段代码:
ViewBag.localitate = db.Localitatis.OrderBy(p => p.Localitate).ToList();
and on the view i try to get it via:
从我的观点来看,我试图通过:
<script type="text/javascript">
$(function () {
var availableTags = [
@foreach (var item in ViewBag.localitate)
{
@item.Localitate
}
];
$("#destination").autocomplete({
source: availableTags
});
});
</script>
I din't know how to format this in order to get an array like in the jquery demo example :
我不知道如何格式化它来获得一个像jquery演示示例中的数组:
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
in order to make the autocomplete work with my values from database,
为了使autocomplete能够使用数据库中的值,
thank you
谢谢你!
2 个解决方案
#1
0
First, set your view data to only have the actual names you want:
首先,将视图数据设置为只有您想要的实际名称:
ViewBag.localitate = db.Localitatis.Select(p => p.Localitate).OrderBy(p => p).ToList();
Then the simplest way to render this in javascript would be:
那么用javascript呈现这个的最简单方法是:
var availableTags = [" @String.Split("\,\"", ViewBag.localitate) "];
But I'd recommend using a JSON parser like JSON.NET instead. This way you don't have to worry about cross-site scripting attacks and such, since things are escaped for you.
但是我建议使用JSON解析器。净。通过这种方式,您不必担心跨站点脚本攻击之类的问题,因为对您来说是转义的。
var availableTags = @(JsonConvert.Serialize(ViewBag.localitate));
#2
2
<script type="text/javascript">
$(function () {
$("#destination").autocomplete({
source: @Html.Raw(Json.Encode(ViewBag.localitate))
});
});
</script>
If you have lots of data, consider using a server-side controller action to perform the filtering.
如果您有很多数据,可以考虑使用服务器端控制器操作来执行过滤。
#1
0
First, set your view data to only have the actual names you want:
首先,将视图数据设置为只有您想要的实际名称:
ViewBag.localitate = db.Localitatis.Select(p => p.Localitate).OrderBy(p => p).ToList();
Then the simplest way to render this in javascript would be:
那么用javascript呈现这个的最简单方法是:
var availableTags = [" @String.Split("\,\"", ViewBag.localitate) "];
But I'd recommend using a JSON parser like JSON.NET instead. This way you don't have to worry about cross-site scripting attacks and such, since things are escaped for you.
但是我建议使用JSON解析器。净。通过这种方式,您不必担心跨站点脚本攻击之类的问题,因为对您来说是转义的。
var availableTags = @(JsonConvert.Serialize(ViewBag.localitate));
#2
2
<script type="text/javascript">
$(function () {
$("#destination").autocomplete({
source: @Html.Raw(Json.Encode(ViewBag.localitate))
});
});
</script>
If you have lots of data, consider using a server-side controller action to perform the filtering.
如果您有很多数据,可以考虑使用服务器端控制器操作来执行过滤。