微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将控制器循环到 Ajax,然后将其与视图绑定?

如何解决如何将控制器循环到 Ajax,然后将其与视图绑定?

下面的代码调用数据库表,但我希望在搜索按钮下方显示值,因为它在我的数据库列表中

我的控制器

 public JsonResult GetTerm(string searchText)
        {
            IEnumerable<FileDetails> fileDetails = new List<FileDetails>();
            var files = (from a in _context.FileDetails
                         where a.FileName.Contains(searchText)
                         select new
                         {
                             a.Imagename,a.FileName,a.CategoriesName
                         }).ToList();
            return Json(files,JsonRequestBehavior.AllowGet);
        }

我的观点

@model IEnumerable<KNowledge.Model.Models.FileDetails>
@{
    ViewBag.Title = "Dashboard";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("Dashboard","FileManagement",FormMethod.Post,new { enctype = "multipart/form-data" }))
{

    <div class="container">
        <div class="form-group">
            <div class="input-group">
                <span class="input-group-addon"><i class="glyphicon glyphicon-search"></i></span>
                <input type="text" id="searchId" placeholder="Search" class="form-control" onInput="edValueKeyPress()" />
            </div>
        </div>
    </div>
    
}

我的 Ajax

function edValueKeyPress() {
    var eValue = document.getElementById("searchId");
    $.ajax({
        type: "GET",url: '/FileManagement/GetTerm',data: { searchText: eValue.value },contentType: "application/json; charset=utf-8",dataType: "json",success: function () {

        },error: function (errormessage) {
            alert(errormessage.responseText);
        }
    });
}

所以我的查询是查看通过 UI 页面Controller 部分传递的值。提前致谢。

解决方法

您有两个选择:

  1. 没有第三方组件。编写您的成功函数,该函数将从接收到的数据生成表。然后用这个表替换某些div。

  2. 使用可以为您完成这项工作的 3rd 方组件。如果你使用 bootstrap,我个人推荐 https://bootstrap-table.com/https://datatables.net/ 这两个组件都有很好的文档。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。