gpt4 book ai didi

jquery - 带有值和文本字段的 ASP.NET MVC jquery 自动完成

转载 作者:行者123 更新时间:2023-12-03 21:54:40 25 4
gpt4 key购买 nike

Controller

public ActionResult Search(string id)
{
id= Request.QueryString["term"];
var routeList = db.Movies.Where(r => r.Title.Contains(id))
.Take(5)
.Select(r => new { id = r.MovieID, label = r.Title, name = "MovieID" });
return Json(routeList, JsonRequestBehavior.AllowGet);
}

查看:

<input type="hidden"   id="MovieID"  name="MovieID" />
<input type="text" id="SelectedMovie" value=""/>
<script type="text/javascript" language="javascript">
$("#SelectedMovie").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Transaction/Search", type: "POST", dataType: "json",
data: { id: request.term },
success: function (data) {
response($.map(data, function (item) {
return { label: item.label, value: item.id }; //updated code
}));
}
});
},
select: function (event, ui) {
$("#MovieID").val(ui.item.value);
$("#SelectedMovie").val(ui.item.label);
return false;
}
});
</script>

我有某种音像商店应用程序。当我去租电影时,我需要一个包含电影的组合框,我可以使用自动完成功能来选择这些电影。另外的要求是只有 ID(值)而不是文本本身保存到数据库中。

编辑:这是完整的工作示例

最佳答案

由于您仅将字符串传递给服务器端的 Search() 函数,因此您通过 $.ajax 传递的 data 元素() 调用需要更改。

public ActionResult Search(string id)//I think that the id that you are passing here needs to be the search term. You may not have to change anything here, but you do in the $.ajax() call
{
id= Request.QueryString["term"];

var routeList = db.Movies.Where(r => r.Title.Contains(id))//this is a text filter no?
.Take(5)
.Select(r => new { id = r.MovieID, label = r.Title, name = "MovieID" });
return Json(routeList, JsonRequestBehavior.AllowGet);
}
<小时/>
$("#MovieID").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Transaction/Search", type: "POST", dataType: "json",
//original code
//data: { searchText: request.id, maxResults: 10 },
//updated code; updated to request.term
//and removed the maxResults since you are not using it on the server side
data: { id: request.term },

success: function (data) {
response($.map(data, function (item) {
//original code
//return { label: item.FullName, value: item.FullName, id: item.TagId };
//updated code
return { label: item.label, value: item.label, id: item.id };
}));
},
select: function (event, ui) {
//update the jQuery selector here to your target hidden field
$("input[type=hidden]").val(ui.item.id);
}
});
},
});
<小时/>

让我知道这是否有效/有帮助!

关于jquery - 带有值和文本字段的 ASP.NET MVC jquery 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12511377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com