【问题标题】:Adding Autocomplete and TagEditor Jquery to MVC将自动完成和 TagEditor Jquery 添加到 MVC
【发布时间】:2019-10-02 17:39:28
【问题描述】:

我希望将 AutoComplete 和 TagEditor 功能结合在一起,从数据库中提取要使用的数据。这将继续以字符串的形式填充一个新条目,但现在我只专注于让这些功能正常工作。

到目前为止,我已经尝试了几种不同的方法,从原始示例到此处的链接,如 jquery-ui-autocomplete-asp-net-mvc5-rendering-as-a-list,但我不会绕圈子。

我有 2 个主要问题,我不确定它们是否相关。

首先,TagEditor 功能在我的网站上作为一个独立字段运行。我设置了一个部分视图作为测试,其中包含 2 个字段。顶部是按预期正确格式化为标记 TextArea,但底部是我试图将其链接到字符串字段的 HTML 帮助器的位置,它不拾取 JQuery 元素。

_SkillSearch.cshtml

@model NR.Models.Critvit

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<hr />
<div class="form-horizontal">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-horizontal">
        <div class="form-group">
            @Html.LabelFor(model => model.KeySkills, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <textarea class="skills"></textarea>
                @Html.ValidationMessageFor(model => model.KeySkills, "", new { @class = "text-danger" })
            </div>
        </div>
        <br />
        <hr />

        <div class="form-group">
            @Html.LabelFor(model => model.KeySkills, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(model => model.KeySkills, new { htmlAttributes = new { id = "skills", @class = "skills form-control" } })
                @Html.ValidationMessageFor(model => model.KeySkills, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
</div>

}

其次,查询不想使用函数中设置的任何 Source 值。这是最新版本,我试图从数据库中获取技能列表并将它们用作源数组。当我运行页面并使用这些字段时,我也没有看到任何 JSON 活动。这是我尝试过的几种不同的方法。

_Layout.cshtml 中的脚本 - 版本 1

<script>
$(document).ready(function () {
    $('#skillslist').tagEditor({
        autocomplete: {
            delay: 0, position: { collision: 'flip' },
            source: function(request, response) {
                $.ajax({
                    type: "POST",
                    url: "/skills/search",
                    dataType: "json",
                    data: {
                        term: request.term
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        alert('Error: ' + xhr.responseText);
                    },
                    success: function(data) {
                        response($.map(data, function(c) {
                            return {
                                label: s.SkillName,
                                value: s.SkillId
                            }
                        }));
                    }
                });
            },
                forceLowercase: false,
                placeholder: 'Skills Titles (placeholder) ...'
            }
        });
});

_Layout.cshtml 中的脚本 - 第 2 版

<script type="text/javascript">
$(document).ready(function () {
    $('.skills').tagEditor({
        autocomplete: {
            delay: 0, position: { collision: 'flip' },
            source: '@Url.Action("GetSkillList")'},
                forceLowercase: false,
                placeholder: 'Skills Titles (placeholder) ...'
        });
});
</script>

家庭控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NR.DAL;
using NR.Models;

namespace NR.Controllers
{
public class HomeController : Controller
{
    private NRContext db = new NRContext();

    public ActionResult Index()
    {
        return View();
    }

    [Cut for space]

    public ActionResult GetSkillList(string term)
    {
        var result = from s in db.Skills
                     where s.SkillName.ToLower().Contains(term)
                     select s.SkillName;
        return Json(result.ToList(), JsonRequestBehavior.AllowGet);
    }
}

}

【问题讨论】:

  • 您的部分文本框有 2 个文本框,都具有相同的 id 属性(无效的 html),因此插件将仅应用于第一个。尝试给他们一个类名(比如)class="skills" 并使用$('.skills').tagEditor({。您的GetSkillList() 方法可能存在问题,因为您没有具体化查询。试试return Json(result.ToList(), JsonRequestBehavior.AllowGet);
  • @StephenMuecke 这对班级有用。我没有意识到这是你可以做的事情。我仍然没有得到自动完成的工作,我已经用我现在拥有的东西在上面进行了编辑。
  • 检查您的浏览器控制台是否有任何错误。
  • Uncaught TypeError: input.autocomplete 不是一个函数。我仔细检查了示例:goodies.pixabay.com/jquery/tag-editor/demo.html (Demo2),发现了一个小错误,但也没有解决。
  • 我也尝试切换到 TagIt 并以原始格式使用 Demo2,但仍然出现相同的错误。 “ input.autocomplete 不是一个函数。” 有什么想法吗?

标签: jquery html asp.net-mvc-5 jquery-tageditor


【解决方案1】:

结束并回答:

@stephenmuecke 让我走上了正确的道路,以确保 Json 包含 ToList 并且捆绑包是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-12
    • 2013-06-22
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多