简体   繁体   English

将自动完成和TagEditor jQuery添加到MVC

[英]Adding Autocomplete and TagEditor Jquery to MVC

I'm looking to combine the AutoComplete and TagEditor features together, drawing the data to be used from a database. 我希望将AutoComplete和TagEditor功能结合在一起,以从数据库中提取要使用的数据。 This is going to go on to populate a new entry as an string, but for now I'm just focusing on getting the features working. 这将继续以字符串形式填充新条目,但现在我仅关注使功能正常工作。

So far, I've tried a couple of different approaches, from both the original examples to links here like jquery-ui-autocomplete-asp-net-mvc5-rendering-as-a-list , but I'm not going around in circles. 到目前为止,我已经尝试了几种不同的方法,从原始示例到此处的链接,例如jquery-ui-autocomplete-asp-net-mvc5-rendering-as-a-list ,但我不会在界。

I have 2 main issues, and I'm not sure if they are linked or not. 我有2个主要问题,并且不确定是否已链接。

Firstly, The TagEditor feature is working on my site as a standalone field. 首先,TagEditor功能在我的网站上作为独立字段运行。 I set up a partial view as a test with 2 fields in place. 我将局部视图设置为具有2个字段的测试。 The top one is formatting correctly as a Tag TextArea as expected, but the bottom one is where I am trying to link this into a HTML helper for a string field, it doesn't pick up the JQuery element. 最上面的一个按预期正确设置为Tag TextArea格式,但是最下面的一个是我试图将其链接到字符串字段的HTML帮助器中,但没有选择JQuery元素。

TextArea问题

_SkillSearch.cshtml _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>

} }

Secondly, the query doesn't want to use any of the Source values set in the function. 其次,查询不想使用函数中设置的任何Source值。 This is the most recent version, where I'm trying to get the list of skills from the db and use them as the Source array's. 这是最新版本,我正尝试从数据库中获取技能列表,并将其用作源数组。 I'm not seeing any JSON activity when I run the page and use the fields either. 运行页面并使用字段时,我没有看到任何JSON活动。 This is a couple of different ways I've tried. 这是我尝试过的几种不同方法。

Script in _Layout.cshtml - Version 1 _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) ...'
            }
        });
});

Script in _Layout.cshtml - Version 2 _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>

Home Controller 家庭控制器

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);
    }
}

} }

To close out and answer: 关闭并回答:

@stephenmuecke got me on the right track to ensure the Json include ToList and the bundle was correct. @stephenmuecke使我处在正确的轨道上,以确保Json包含ToList并且捆绑包正确。

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

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