简体   繁体   English

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

[英]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. 如果要在与搜索条件文本框相同的视图页面中显示结果,则更好的方法是使用JQuery表单发布。 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. 如果使用任何第三方网格,则可以从相应的控制器中获取JSON结果并将其绑定到网格。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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