火从一个jQuery自动完成选择一个控制器的动作 [英] Fire a controller action from a jQuery Autocomplete selection
问题描述
我有一个jQuery自动选择框,显示正确的数据。现在我想,当一个项目被选中时触发ASP.NET MVC 3控制器。然后,控制器应该重定向到一个视图。这里是我的jQuery自动完成code(我敢肯定缺少的东西在第二Ajax调用,但我还没有找到它):
I have a jQuery autoselect box displaying the correct data. Now I would like to fire an ASP.NET MVC 3 controller when an item is selected. The controller should then redirect to a View. Here's my jQuery autocomplete code (I'm sure something is missing in the 2nd Ajax call, but I haven't found it yet):
<script type="text/javascript">
$(function () {
$("#Client").autocomplete({
source: function (request, response) {
$.ajax({
url: 'Entity/GetClientAutoComplete', type: 'POST', dataType: 'json',
data: { query: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item, value: item };
}))
}
})
},
minLength: 1,
select: function (event, ui) {
$.ajax({
url: 'Entity/GetApplicationsByName/' + ui.item.value, type: 'POST'
})
}
});
});
</script>
和这里就是我试图调用控制器:
And here's the controller I'm trying to call:
public ActionResult GetApplicationsByName(string id)
{
ViewBag.Client = id;
var apps = _service.GetDashboardByName(id);
return View("Dashboard", apps.ToList());
}
当我看到在Firebug Ajax调用火,我看到正确的URL的配置,但没有什么事情发生。它的作用就好像它想要的东西加载,而不是送东西。我很困惑。感谢您的任何指导。
When I watch the Ajax call fire in Firebug, I see the correct URL configuration, but nothing else happens. It's acting as though it wants to load something rather than send something. I'm confused. Thank you for any guidance.
推荐答案
嗯,你发了 ID
按 POST
在 GetApplicationsByName
控制器和控制器发回的看法。
Well you sent an id
by POST
to the GetApplicationsByName
controller and the controller is sending back the view.
如果你想重定向,可以使用以下内容:
If you want redirection, you can use the following:
select: function (event, ui) {
window.location.href = 'Entity/GetApplicationsByName/' + ui.item.value;
}
这篇关于火从一个jQuery自动完成选择一个控制器的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!