【问题标题】:How can we filter the records in a strongly typed partial view in ASP.NET MVC?我们如何在 ASP.NET MVC 的强类型局部视图中过滤记录?
【发布时间】:2011-01-06 14:50:02
【问题描述】:

我有一个强类型的局部视图,它填充了搜索表中的所有记录。 现在我有一个文本框来输入名称和一个按钮来过滤可以匹配名称的记录。(如搜索页面)。

谁能提供这个场景的代码示例?

我的主页使用 JQuery 加载基于单选按钮选择(搜索或查询)的特定视图,如下所示:

/* 加载基于单选按钮点击的局部视图... */

        $(document).ready(function() {
            $(':radio').click(function() {
                if (this.value == '2') {
                $('#ViewAllInquiries').load('/Home/Inquiry', function(html) { $('#ViewAllInquiries')[0].value = html; });
                }
                else {
                    $('#ViewAllInquiries').load('/Home/Search', function(html) { $('#ViewAllInquiries')[0].value = html; });
                }
            });
        })

这是我的部分视图 ControllerCode 之一:

[HttpGet]

    public ActionResult Search()
    {
        var search = from s in entity.Search
                     select s; return PartialView(search);
    }

这是用户控件部分视图(Search.ascx):

>" %>
<table >
<thead>
    <tr>
        <th align="left"> </th>
        <th align="left"> TX_Id</th>
        <th align="left">Name&nbsp;
           <%= Html.TextBox("Name")%>&nbsp;<input type="submit" value="Filter" /></th>
        <th align="left">Email Address</th>
     </tr>

">

感谢您的宝贵时间。

【问题讨论】:

  • 您是否仅限于使用 jQuery 或者您是否愿意使用 MS Ajax 表单?如果您使用 Ajax 表单,您的代码可以大大简化。
  • 我对 Ajax 表单持开放态度。但我是 MVC 本身的新手。你能帮我用 Ajax 编写代码示例吗?感谢您的时间。
  • 我已经把答案写进去了,希望对您有所帮助。

标签: asp.net-mvc


【解决方案1】:

我使用 Ajax 表单做同样的事情。这真的很容易。这是我使用的代码:

HTML:

<div>
    <% 
        using (Ajax.BeginForm("Home", "Search", null,
            new AjaxOptions { UpdateTargetId = "Output" }, 
            new { id = "SearchForm" }))
        {
            %>

                <!-- Form Fields --> 
                <input name="searchField" />
                <input type="submit" value="Search" />   
            <%
        } 
    %>

    <div id="Output">

    </div>

</div>

然后在你刚刚拥有的控制器中:

public PartialViewResult Search(FormCollection form)
{

    var model = YourSearchMethod(form["searchField"]);
    return PartialView("Search", model);

}

每次单击提交按钮时,ID 为“Output”的 div 都会更新为您的部分视图结果。在您的情况下,您有两个不同的潜在部分视图,只需将单选按钮值作为表单的一部分提交,您就可以从控制器内切换输出视图。

为什么使用 FormCollection 而不是参数?我在使用带有 ajax 表单的命名参数时遇到了一些困难,但是您可以尝试一下,看看它是如何工作的。它应该看起来像这样:

public PartialViewResult Search(string searchField, bool inquiry)
{
    if (inquiry)
    {
        var model = YourInquiryMethod(searchField);
        return PartialView("Inquiry", model);
    }
    else
    {
        var model = YourSearchMethod(searchField);
        return PartialView("Search", model);
    }
}

【讨论】:

  • 看起来很有趣。将尝试 n 让你很快知道结果:-)
  • 在单击搜索字段文本框时获取 Javascript 错误消息。
  • 你能说得更具体点吗?另外,请确保您的页面中包含 ms ajax java 脚本文件。
【解决方案2】:

我在我的一个网站上做了同样的事情,但实现起来有点不同。

我的视图中有以下 html;

<div class="EditProductContainer hidden"></div>

我也有下面的jQuery;

 function editBenefit(objThis) {
     var id = $(objThis).parents('.Benefit').attr("id");
     $.post("/Home/jQueryGetBenefit", { Id: id },
        function(newHTML) {
            $('.EditProductContainer').html(newHTML);
        });
 }

然后在我的控制器中我有;

    [AcceptVerbs(HttpVerbs.Post)]
    public PartialViewResult jQueryGetBenefit(int Id)
    {
        Application application = Helpers.CacheHelper.Get();
        Benefit thisBenefit = application.findBenefit(Id);

        return PartialView("EditBenefit", thisBenefit);
    }

我认为这是在做你想做的事,但我正在返回一个渲染的 PartialView 并用部分视图生成的 html 替换包含 div 的内容。

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    相关资源
    最近更新 更多