如何在asp.net MVC中使用Ajax进行搜索 [英] How to search using Ajax in asp.net MVC

查看:76
本文介绍了如何在asp.net MVC中使用Ajax进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图刷新一个表而不使用Ajax加载整个页面.当我调用PartialViewResult时,它不会从文本框中获取字符串,而是进行空搜索",并返回表中的所有值,而不是我要搜索的特定值.

I am trying to refresh a table without loading the entire page using Ajax. When I call the PartialViewResult it doesn't fetch the string from the textbox and does a "empty search" and returns all the values in the table instead of the specific that I am searching for.

如何获取html文本框内的字符串并将其用于PartialVewResult中,以仅获取我搜索的结果?

HTML代码(LogLayout.cshtml)

    @using (Ajax.BeginForm("LogPartialView", "LogModelsController",
                new AjaxOptions
                {
                    HttpMethod = "POST",
                    InsertionMode = InsertionMode.Replace,
                    UpdateTargetId = "divLogs"
                }))
                                {
                                    <p>
                                        <b>Message:</b> @Html.TextBox("SearchString")
                                        <input type="submit" id="submit" name="submit" value="Search" />

                                    </p>
                                }<div id="divLogs">
                                    @RenderBody()
                                    @Html.Raw(ViewBag.Data)
                                </div>

控制器(LogModelsController)

 public PartialViewResult LogPartialView(string searchString, string currentFilter, int? page, string sortOrder)
        {
            ViewBag.CurrentSort = sortOrder;

            string myID = "0";
            if (Session["myID"] != null)
            {
                myID = Session["myID"].ToString();
            }
            int testID;
            try
            {
                testID = Convert.ToInt32(myID);
            }
            catch (FormatException ex)
            {
                throw new FormatException("FormatException error in LogModelsController", ex);
            }

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;
            //Linq code that gets data from the database
            var message = (from log in db.Log
                           where log.customerId == testID
                           select log);

            if (!String.IsNullOrEmpty(searchString))
            {
                message = message.Where(s => s.message.Contains(searchString));
            }

            //PageSize displays the maximum number of rows on each page
            int pageSize = 10;
            int pageNumber = (page ?? 1);

            return PartialView("_LogPartialLayout", message.OrderByDescending(i => i.timeStamp).ToPagedList(pageNumber, pageSize));
        }
    }
}

部分布局(_LogPartialLayout.cshtml)

@model PagedList.IPagedList<ASDMVC.Models.LogModel>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

<table class="table">
    <tr>
        <th>
            @Html.ActionLink("message", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })

        </th>
        <th>
            @Html.ActionLink("timestamp", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.ActionLink("level", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>

    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.message)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.timeStamp)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.level)
            </td>
        </tr>
    }
</table>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

推荐答案

我找到了解决方案!:)

I found a solution!:)

我必须为我的表添加一个新的ID:

I had to add a new id for my table:

 @using (Ajax.BeginForm("LogPartialView", "LogModelsController",
          new AjaxOptions
          {
          HttpMethod = "POST",
          InsertionMode = InsertionMode.Replace,
          UpdateTargetId = "divLogs"
      }, new
      {
         id = "NewTableId"
       )){
     <p>
       <b>Message:</b> @Html.TextArea("SearchString")
       <input type="submit" id="submit" name="submit" value="Search" />
     </p>
     }<div id="divLogs">
     @RenderBody()
     @Html.Raw(ViewBag.Data)
     </div>

这篇关于如何在asp.net MVC中使用Ajax进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆