簡體   English   中英

搜索和結果在asp.net MVC C#

[英]search and result in asp.net mvc c#

我有一個帶有按鈕搜索的文本框。 我想從文本框中搜索輸入文本的數據庫,並在一個視圖中給出結果。 您能否給我鏈接或幫助我如何實現它? 我不知道我該怎么做。

如果要在與搜索條件文本框相同的視圖頁面中顯示結果,則更好的方法是使用JQuery表單發布。 如果使用任何第三方網格,則可以從相應的控制器中獲取JSON結果並將其綁定到網格。

發布機制將是:

      <div>
            <input type="text" id="txtSearchText" value=""/>
            <input type="button" id="btnSearch" value="Search"/>
      </div>
            <script type="text/javascript">
               $("#btnSearch").click(function(){
                  var formPost={ SearchText : $("#txtSearchText").val()}; 
//The above SearchText parameter name should be same as property name in the Model class.
                  $.post("/SearchController/Search",formPost,function(data){
                  if(data)
                  {
                     //Here based on your development methodology, either build a table by    
                      //appending a row for each result Or bind to a grid, if you are using  
                      //any third party control
                  }
                  });
               });

            </script>

控制器代碼:

public class SearchController
{
    public ActionMethod Search(SearchModel searchCriteria)
    {
           SearchResultModel result= //Get the search results from database
           return new JsonResult{Data=result};
    }
}

型號類別:

public class SearchModel
{
    public string SearchText{get;set;};
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM