2015-09-25 91 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>