【问题标题】:How to Loop a Controller to Ajax and then bind it with View?如何将控制器循环到 Ajax,然后将其与 View 绑定?
【发布时间】:2021-04-08 06:45:14
【问题描述】:

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

我的控制器

 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 部分的值。提前致谢。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-ajax


    【解决方案1】:

    你有两个选择:

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

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

    【讨论】:

    • 谢谢您的回答先生。但是你能给我一个例子来说明我如何使用第一个选项来实现吗?提前致谢!
    猜你喜欢
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 2015-12-31
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    相关资源
    最近更新 更多