简体   繁体   English

使用JS和ASP.NET MVC的Web网格搜索功能

[英]Web grid search function using JS and ASP.NET MVC

I am using ASP.NET MVC. 我正在使用ASP.NET MVC。 I try to implement search box . 我尝试实现搜索框

I have a input box and a button: 我有一个输入框和一个按钮:

<div>
    <input type="text" name="Search" id="Search" />
    <input type="submit" value="Search" id="SearchButton" />
</div>

Then I use JS to receive user input and trigger a function: 然后我使用JS接收用户输入并触发一个函数:

$(document).ready(function () {
    $('#SearchButton').click(function Search() {
        $.ajax({
            url: '@Url.Action("Search", "PCA")',
            data: { AutoCribItemID: $('#Search').val() },
            type: "POST",
            dataType: "json",
            success: function (result) {
                var data = new Array();
                for (var i = 0; i < result.Data.length; i++) {
                    data.push(result.Data[i]);
                }

                loadData(data);
            },
            error: function () {
                alert("Failed! Please try again.");
            }
        });
    });
});

Then in back end, there is a respond method: 然后在后端,有一个响应方法:

[HttpPost, ActionName("Search")]
public JsonResult Search(string AutoCribItemID) {
    List<PCAModel> allRecords = new List<PCAModel>();
    allRecords = db.elements.Where(model => model.AutoCribItemID.Contains(AutoCribItemID)).ToList();
    return Json(new { Data = allRecords }, JsonRequestBehavior.AllowGet);
}

You can see this C# method returns a list to frontend. 您可以看到此C#方法将列表返回到前端。 Then what I want to do is to show all data after searching to user. 然后我想要做的是在搜索到用户后显示所有数据。

Here is a method to show data after searching: 这是搜索后显示数据的方法:

function loadData(result) {
    var table = $('<table id="indexTable"></table>');
    var thead = $('<thead></thead>');
    var trow = $('<tr class="header"></tr>');
    trow.append('<th>EffectiveDate</th>');
    trow.append('<th>ChangeAgree</th>');
    trow.append('<th>Client</th>');
    trow.append('<th>Installation</th>');
    trow.append('<th>AutoCribItemID</th>');
    trow.append('<th>RGBSupplier</th>');
    trow.append('<th>Price</th>');
    trow.append('<th>SubmitDate</th>');
    trow.append('<th>WINUserName</th>');
    trow.append('<th>Export</th>');
    thead.append(trow);
    table.append(thead);
    var tbody = $('<tbody></tbody>');
    for (var i = 0; i < result.length; i++) {
        tbody.append(result[i]);
    }

    table.append(tbody);

    $('#indexTableBody').html(table);
    location.reload();
}

Note: 'result' parameter above is from C# function from backend. 注意:上面的“结果”参数来自后端的C#函数。

The question is, web page shows all record in the table instead of showing searched data. 问题是,网页显示表中的所有记录,而不是显示搜索的数据。 I only need to show data after searching in web grid. 我只需要在网格搜索后显示数据。

Without knowing the structure of your AutoCribItemID's, my guess is that because you're using .Contains in your where clause it's returning anything that matches that search term. 在不知道AutoCribItemID的结构的情况下,我的猜测是,因为你在where子句中使用.Contains,它会返回与该搜索词匹配的任何东西 So, for example, if you had integer IDs and someone searched for "1", it would return any ID that contains the number 1 including 1, 10, 11, 12, 13, etc. You probably want to use an equality comparison instead, ie model.AutoCribItemID == AutoCribItemID 因此,例如,如果您有整数ID并且有人搜索“1”,它将返回包含数字1的任何ID,包括1,10,11,12,13等。您可能希望使用相等比较来代替,即model.AutoCribItemID == AutoCribItemID

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

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