如何从自动完成而不是文本发布值?

时间:2017-01-24 07:37:31

标签: javascript c# jquery asp.net-mvc jquery-ui-autocomplete

这是我的C#代码:

SetupDiSetDeviceRegistryProperty

HTML代码:

/**
* A callback function called once all tests have finished running and
* the WebDriver instance has been shut down. It is passed the exit 
  code
* (0 if the tests passed). afterLaunch must return a promise if you 
  want
* asynchronous code to be executed before the program exits.
* This is called only once before the program exits (after onCleanUp).
*/

Javascript代码:

public JsonResult FillUsers(string term)
    {
        var Retailers = from us in db.Users
                        join pi in db.UserPersonalInfoes on us.ID equals pi.UserID into t
                        from rt in t.DefaultIfEmpty()
                        where us.Status == true
                        select new
                        {
                            ID = us.ID,
                            Username = us.Username + ":( " + (rt == null ? String.Empty : rt.FirstName) + " )"
                        };

        List<string> UsersList;
        UsersList = Retailers.Where(x => x.Username.Contains(term)).Select(y => y.Username).Take(10).ToList();

        return Json(UsersList, JsonRequestBehavior.AllowGet);
    }

我想在文本字段中显示'用户名',但是当表单发布时我想发送'ID'。而不是我得到用户名。

2 个答案:

答案 0 :(得分:0)

为了实现这一目标,您可能需要一个额外的隐藏字段,在进行选择时存储id:

$("#ddlUser").autocomplete({
    source: '@Url.Action("FillUsers", "FirebaseNotification")',
    select: function(event, ui) {
        var id = ui.item.ID;
        $('#selectedUserId').val(id);
    }
});

现在提交表单时,您将忽略来自文本输入字段的username值,而是隐藏字段值:

@Html.Hidden("selectedUserId", null, new { id = "selectedUserId" })

答案 1 :(得分:0)

$("#ddlUser").keyup(function (e) { 
  if (e.which != 13) { 
    $("#hfUserID").val("0"); 
  } 
  $("#ddlUser").autocomplete({ 
    source: function (request, response) { ... }, 
    select: function (e, i) {   
      $("#hfUserID").val(i.item.val);
      $("#ddlUser").val(i.item.label); 
    }, 
    minLength: 1 
                                         
    }); 
  });