【问题标题】:MVC AutocompleteMVC 自动完成
【发布时间】:2015-03-12 07:00:19
【问题描述】:

根据我的研究,我尝试了 MVC C# 的自动完成代码,它可以工作。代码如下:

对于脚本:

$(document).ready(function () {
    $("#Users").autocomplete({
        source: function(request,response) {
            $.ajax({
                url: "/Home/AutoCompleteUsers",
                type: "POST",
                dataType: "json",
                data: { term: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.Name, value: item.ID };
                    }))

                }
            })
        },
        messages: {
            noResults: "", results: ""
        }
    });
})

观点:

Enter Name: @Html.TextBox("Employees")
<input type="submit" id="GetEmployees" value="Submit"/>

在控制器中:

public JsonResult AutoCompleteUsers(string term)
{
    var result = (from r in db.Users
    where r.Name.ToLower().Contains(term.ToLower())
    select new { r.Name, r.ID }).Distinct();
    return Json(result, JsonRequestBehavior.AllowGet);
}

我的问题是:

我想知道如何获取对应的ID并将其放入表单的输入字段中,以便将ID包含在提交的表单中。非常感谢任何帮助。

【问题讨论】:

  • 文本框有model property吗?

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


【解决方案1】:

您需要使用自动完成的select 属性并告诉它当用户从自动完成中选择一个项目时要做什么:

select: function (event, ui) {
        $("#Employees").val(ui.item.value); //setting id in Employees TextBox

    }

更好的是在 TextBox 中设置员工姓名并在隐藏字段中设置 id 并在表单发布时从隐藏字段中检索 id:

Enter Name: @Html.TextBox("Employees")
<input id="EmployeeId" type="hidden"/>
<input type="submit" id="GetEmployees" value="Submit"/>

然后:

select: function (event, ui) {
            $("#Employees").val(ui.item.label); 
            $("#EmployeeId").val(ui.item.value);

        }

选择(事件,用户界面):

从菜单中选择项目时触发。默认操作是将文本字段的值替换为所选项目的值。

【讨论】:

  • 谢谢。它显示了 EmployeeID 中每个名称的相应 ID,但是一旦我选择了列表中的一个,文本框字段就会被替换为 id。为什么会这样?
  • 没听懂你说的
【解决方案2】:
@Html.Hidden("EmployeeId")
$(document).ready(function () {
$("#Users").autocomplete({
    source: function(request,response) {
        $.ajax({
            url: "/Home/AutoCompleteUsers",
            type: "POST",
            dataType: "json",
            data: { term: request.term },
            success: function (data) {
                response($.map(data, function (item) {
                    return { label: item.Name, value: item.ID };
                }))

            }
        })
    },
    select: function (event, ui) {
      event.preventdefault().
      $("#EmployeeId").val(ui.item.value);
      $("#Employees").val(ui.item.label);
    },
    messages: {
        noResults: "", results: ""
    }
});
})

【讨论】:

  • 我尝试在表单中发送它并将“员工”传递给将显示相应 ID 以显示 ID 但值为 0 的操作。我的操作接受员工作为参数 int,因为它的值是身份证。我将它查询到表并循环它。我使用 Viewbag 显示该特定 ID 的名称并返回查看。问题是#Employees 的值为0。
  • 查看编辑后的答案。确保在控制器操作中正确映射#EmployeeId
  • 我已经得到了它,但是每当我从文本框字段中选择一个时,它将被替换为员工 ID 字段中的 ID。为什么?
  • 我已经得到答案了,我刚刚补充了:event.preventdefault()..谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 2011-01-24
  • 2011-04-15
  • 2012-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多