【问题标题】:Search and display on same page in mvc2 asp net在 mvc2 asp net 的同一页面上搜索并显示
【发布时间】:2011-09-26 16:39:39
【问题描述】:

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

现在我正在执行以下操作,但效果并不完全符合我的要求:

【问题讨论】:

  • 您忘记包含您的代码。
  • 这是两个不同的动作,有两个不同的模型。为什么不将它们放在单独的页面上,或者通过 AJAX 调用加载它。

标签: asp.net-mvc jquery


【解决方案1】:

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

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

<div id="searchResults"></div>

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

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

然后,您需要添加一个局部视图 SearchPartial.ascx,其中包含您的搜索结果模型。

希望这会有所帮助。

【解决方案2】:

您可以使用Ajax解决问题。

<div>
`@using (Ajax.BeginForm("action", "controller", new AjaxOptions
    {
        UpdateTargetId = "results",
        HttpMethod = "GET",
    }))
    {
        @Html.TextBox()
        <input type="submit" value="Search" />
    }`
<div id="results"></div>
</div>

【讨论】:

    猜你喜欢
    • 2016-06-25
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 2015-11-26
    相关资源
    最近更新 更多