在View界面添加如下js代码:
<label> <span>用户名:</span> @Html.TextBox("uesrName", null, new { @class = "m-wrap small" }) </label> $(function () { $("#uesrName").autocomplete({ minLength: 0, autoFocus:true , matchCase:true, autoFill: true, selectFirst: true, source: function (request, response) { $.ajax({ url: '@Url.Action("getUserInfoList", "ErrorCode")', type: "POST", dataType: "json", data: { uesrName: request.term}, success: function (data) { response( $.map( data, function (item) { return { label: item.userName, value: item.deviceID, value2: item.userID } } //end function (item) )//end map ); //end response } //end success }); //end ajax }, //end source focus: function (event, ui) { //$(this).val(ui.item.label); $("#diviceId").val(ui.item.value); $("#clientId").val(ui.item.value2); $('#exitUser').val(ui.item.label); return false; }, //end focus select: function (event, ui) { $(this).val(ui.item.label); $("#diviceId").val(ui.item.value); $("#clientId").val(ui.item.value2); $('#exitUser').val(ui.item.label); return false; } //end select }); //end autocomplete }); //end function
var b = $.map( [55,1,2], function( item,index ) { return { "label": item, "value": index }}); alert(b[0].label +" "+ b[0].value); //输出为 55 0 var array = [0, 1, 52, 97]; array = $.map(array, function(a, index) { return [a - 45, index]; }); //输出为:[-45, 0, -44, 1, 7, 2, 52, 3]
#region 根据用户名检索用户信息 /// <summary> /// 根据用户名检索用户信息(错误码添加页面) /// Ajax请求 /// </summary> /// <param name="uesrName"></param> /// <returns></returns> [HttpPost] public ActionResult getUserInfoList(string uesrName) { ClientRequest request = new ClientRequest(); var result = this.ClientService.GetOnlyClientList(request); var formateData = result.AsEnumerable() .Select(x => new { userName = x.login_name, userID = x.ID, //防止取得的Client没有分配gateway deviceID = x.gateway.client_uid }); if (!string.IsNullOrEmpty(uesrName)) { formateData = formateData .Where(x => x.userName.Contains(uesrName.Trim())); } return Json(formateData, JsonRequestBehavior.AllowGet); } #endregion