在mvc2 asp net中搜索并显示在同一页面上

Sat*_*ish 0 asp.net-mvc jquery

有一个带文本框的简单搜索表单.在提交表单后,我将文本框的内容发送到存储过程,然后返回给我结果.我希望结果显示在表单的同一页面上,除了它下面.

现在我正在做以下事情,但它并没有按照我想要的方式运作:

jim*_*lan 6

sathishkumar,

您不会将问题标记为与ajax相关的解决方案.我将提出一个简单的ajax方法,它可能适合也可能不适合.

在控制器中:

public ActionResult Search(string searchTerm)
{
    // you don't add code re your stored proc, so here is a repo based approach
    var searchItems = _repository.Find(x => x.searchfield.Contains(searchTerm));
    return PartialView("SearchPartial", searchItems);
}
Run Code Online (Sandbox Code Playgroud)

主视图(index.aspx或其他)(在主要内容定义的下方,添加):

<div id="searchResults"></div>
Run Code Online (Sandbox Code Playgroud)

在页面的另一部分(半伪代码):

<script type="text/javascript">
     function getSearchResults() {
         // #yoursearchbox is a textbox on the index.aspx aview
         var tdata = { searchTerm: $('#yoursearchbox').val()}; 
         // or your data in the format that will be used ??
         $.ajax({
           type: "GET",
           data: tdata,
           url : '<%= Url.Action("Search", "Home") %>',
           success: function (result) { success(result); }  
        });
      }); 

    function success(result){
        $("#searchResults").html(result);
    }
</script>
Run Code Online (Sandbox Code Playgroud)

然后,您将添加包含搜索结果模型的部分视图SearchPartial.ascx.

希望这可以帮助.