简体   繁体   English

自动完成Ajax asp.net MVC,显示文字

[英]Autocomplete Ajax asp.net MVC, Display text

I am using Jquery with Ajax AutoComplete to return some data, but I'm kind of stuck with the display text. 我正在将Jquery与Ajax AutoComplete一起使用以返回一些数据,但是我有点受显示文本的困扰。 This is what I have: 这就是我所拥有的:

<script type="text/javascript">
$(document).ready(function () {
    $("#locais").autocomplete({
    source: function (request, response) {
            $.ajax({
                url: '@Url.Action("CreateF")',
                datatype: "json",
                data: {

                    s_localidade: request.term
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.remetente,
                            value: item.ID,
                        }
                    }))
                }
            })
        },
    });
});

And on controller: 并在控制器上:

   public JsonResult CreateF(string s_localidade)
    {
        var tblOficiosExpedidos = db.tblOficiosExpedidos.Include(t => t.tblLocalidades);
        var local = (from r in db.tblLocalidades
                     where r.Remetente.Contains(s_localidade.ToUpper())
                     select new { remetente = r.Remetente + " - " + r.Cidade, ID = r.LocalidadeID }).ToList();
         return Json(local, JsonRequestBehavior.AllowGet);

    }

On View: 查看时:

@Html.TextBox("LocalidadeID","", htmlAttributes: new { @class = "form-control", @id="locais"})

It does work, but when i select an item, the text of the text box becomes the key number from my table. 它确实起作用,但是当我选择一个项目时,文本框的文本将成为我表中的键号。 Is there a way to when i select something, the text keep as item.remetente but saves the number of item.ID into table? 当我选择某物时,是否有办法将文本保留为item.remetente,但将item.ID的数量保存到表中?

You can do with the help of 'select' try below approach 您可以借助'select'尝试以下方法来完成

Your view 您的看法

@Html.TextBox("LocalidadeID","", htmlAttributes: new { @class = "form-control", @id="locais"})
@Html.Hidden("hiddenlocais")

In your jquery define common function that takes both hidden and text box id as parameters 在您的jquery中,定义将隐藏ID和文本框ID作为参数的通用函数

JQuery: jQuery的:

function WireUpAutoComplete(displayControlId, valueControlId)
 {
  $(displayControlId).autocomplete({
    source: function (request, response) {
            $.ajax({
                url: '@Url.Action("CreateF")',
                datatype: "json",
                data: {
                    s_localidade: request.term
                },
                 select: function (event, ui) {
                      $(displayControlId).val(ui.item.Remetente);
                      $(valueControlId).val(ui.item.ID);
                    return false;
               },  
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.remetente,
                            value: item.ID,
                        }
                    }))
                }
            })
        },
    });

  }

In document ready call above function document ready调用上面的功能

<script type="text/javascript">
$(document).ready(function () {
  WireUpAutoComplete("#locais", "#hiddenlocais");
});

</script>

hope this solve your problem. 希望这能解决您的问题。 i haven't tested. 我还没有测试。 let me know if you face any problems 让我知道你是否遇到任何问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM