ASP.NET MVC jquery自动填充值和文本字段
发布时间:2020-05-24 01:41:51 所属栏目:asp.Net 来源:互联网
导读:调节器 public ActionResult Search(string id){ id= Request.QueryString[term]; var routeList = db.Movies.Where(r = r.Title.Contains(id)) .Take(5)
|
调节器 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>
我有一些视频商店应用程序当我去租一部电影时,我需要一个带有电影的组合框,我可以使用自动完成来选择. 编辑:这里是完整的工作示例 解决方法由于您只将一个字符串传递到服务器端的Search()函数,所以您要通过$.ajax()调用传递的数据元素需要更改.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,name = "MovieID" });
return Json(routeList,JsonRequestBehavior.AllowGet);
}
$("#MovieID").autocomplete({
source: function (request,response) {
$.ajax({
url: "/Transaction/Search",//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 };
}));
},ui) {
//update the jQuery selector here to your target hidden field
$("input[type=hidden]").val(ui.item.id);
}
});
},});
让我知道这是否有效/有帮助! (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 模型项的类型为CookMeIndexViewModel,但需要一个
- asp.net-web-api – 标题中的API密钥与swashbuckle
- 在ASP.NET中添加动态控件,1.1和2.0之间有区别吗?
- 在ASP.NET中如何识别/处理404异常?
- asp.net-mvc – ASP.net MVC返回文件和重定向
- asp.net-mvc – 验证asp.net MVC中的只读输入不显眼的验证
- asp.net-mvc – 在发送到视图之前如何修改控制器动作中的表
- ASP.NET报告系统
- 如何在ASP.net runat =’server’标记中使用传统的HTML id属
- asp.net – 防止用户在同一行上工作
推荐文章
站长推荐
- 如何知道asp.net 3.5 sp1和asp.net mvc是否安装在
- asp.net-mvc – IIS 7上的ASP.net MVC返回空白页
- asp.net-mvc-4 – 基于角色的导航
- asp经典 – ASP Classic中的注释代码
- asp.net-mvc – UnitOfWork in Action Filter似乎
- 如何在asp.net MVC 3中获取当前的视图名称?
- asp.net-mvc-2 – ASP.NET MVC数据注释客户端验证
- asp-classic – 经典的ASP页面是否会运行在Windo
- asp.net-mvc – Href和Url.Content有什么区别?
- ASP.NET平台有相当于Heroku吗?
热点阅读
