简体   繁体   中英

search and result in asp.net mvc c#

i have a textbox with button search. i would like to search my database of input text from textbox and give result out in one view. Could you please give me link or help me how its possible to implement? I have no idea what i shoul to do that.

If you want to show the result in the same view page as that of the search criteria text box, the better approach will be to go with JQuery form post. And from the corresponding controller you can get back the JSON result and bind it to the grid, if you are using any third party grid.

The posting mechanism will be :

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

Controller Code :

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

Model class:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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