2015-09-25 81 views
1

我正在尝试刷新表而不使用Ajax加载整个页面。当我调用PartialViewResult时,它不会从文本框中获取字符串,并执行“空搜索”并返回表中的所有值,而不是我正在搜索的特定值。如何在asp.net中使用Ajax进行搜索MVC

如何获取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 })) 
+1

参数'串searchString'包含文本框的值,但随后你将其覆盖通过使用'searchString = currentFilter;'并且由于您不会为'currentFilter'发布一个值,那么'searchString'就会变为'null'。它有点不清楚你想要的实际逻辑是什么。 –

+0

请详细解释一下 –

+0

我在哪里覆盖搜索字符串?如果搜索字符串(@ Html.Textbox为空/空),我只覆盖它? 当我在文本框中输入一些搜索字符串时,我想从我的日志数据库中将数据提取到_LogLayout页面中的表中。我不想更新整个页面。 –

回答

1

我找到了解决办法:)

我不得不添加一个新的ID为我的表:

@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>