ASP.NET MVC jquery自动填充值和文本字段
发布时间:2020-12-15 23:33:49 所属栏目:asp.Net 来源:网络整理
导读:调节器 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,J
调节器
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 – Javascript之前asp:ButtonField点击
- ASP.NET Active Directory成员资格提供程序和SQL配置文件提
- asp.net-mvc – ASP.NET MVC 5,Identity,Unity容器解决方案
- MVC3 .NET会话随机丢失会话值并返回null
- asp.net – IIS需要很长时间才能首次运行
- ASP.Net Web应用程序安全性不适用于IIS 7?
- asp.net-mvc – OnModelCreating与数据库配置的DataAnnotat
- asp.net – ReportViewer – 隐藏PDF导出
- 检测asp.net将用户重定向到登录页面
- asp.net-mvc – 在ASP.NET MVC中重定向后发布
推荐文章
站长推荐
- asp.net-mvc-3 – ASP.NET MVC 3.0 Razor,从任何
- asp.net – Facebook API – 签名请求无效. (签名
- ASP.NET MVC twitter / myspace样式路由
- asp.net-mvc – MVC店面 – 科纳 – 他们在哪里?
- asp.net – Simplemembership – 添加电子邮件字
- asp.net – 在web.config location元素中无法识别
- asp.net – Orchard CMS如何进行日志记录?
- 在ASP.net Webforms中,如何检测有人按下哪个文本
- asp.net – 有没有办法在Web Api控制器中处理表单
- asp.net-mvc – ASP MVC区域和ActionLink
热点阅读