【问题标题】:Can I use a @Html.PagedListPage with a @url.Action post我可以将@Html.PagedListPage 与@url.Action 帖子一起使用吗
【发布时间】:2017-04-10 20:23:16
【问题描述】:

我想要做的是我有一个输入框,用作“搜索字符串”,它是搜索操作的 POST,我想在同一屏幕上显示结果,这很好用,除了一个例外,当我尝试使用 Pageing 控件时,“@html.PagedList。该控件不断返回到“[httpGet] 搜索而不是帖子。

代码如下:

public ActionResult Search()
    {
        return View();
    }

[HttpPost]
public ActionResult Search(FormCollection fc, int? pageNumber)
{
    var searchString = fc["searchString"];
    var results = new ArrayList();
    var mylist = new List<SearchResult>();
    var model = new SearchViewModel();

    // Search Communities
    var search = from s in db.Communities
                 where s.ComunityName.Contains(searchString) || s.Description.Contains(searchString)
                 select s;

    // Search Resource Center
    var docs = from d in db.ResourceCenters
               where d.Title.Contains(searchString) || d.Description.Contains(searchString)
               select d;

    // Set up Arraylist with type Community
    foreach(var c in search)
    {
        var community = new SearchResult();
        community.type = "community";
        community.CommunityId = c.CommunityId;
        community.CommunityName = c.ComunityName;
        community.Description = c.Description;
        community.CommunityType = c.CommunityType1.TypeName;
        community.CommunityCity = c.CommunityCity;
        community.CommunityState = c.CommunityState;
        community.CommunityZip = c.CommunityZip;
        community.Population = c.Population;
        mylist.Add(community);
    }

    // Set up ArrayList with type ResourceCenter
    foreach (var d in docs)
    {
        var document = new SearchResult();
        document.type = "document";
        document.Title = d.Title;
        document.Document_Description = d.Description;
        document.FilePath = d.FilePath;
        document.Date = Convert.ToDateTime(d.Date);
        document.UpLoadedBy = d.UpLoadedBy;
        mylist.Add(document);
    }

    model.results = mylist;
    ViewBag.results = model.results;
    ViewBag.searchString = searchString;

    return View(mylist.ToPagedList(pageNumber ?? 1, 5));

和标记:

@using (Html.BeginForm("Search", "Home", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    <input type="text" id="searchString" name="searchString" class="form-control" required="required" />
    <input type="submit" class="btn btn-default" value="Search" />
    <hr />


    if (@Model != null)
    {
        if (@Model.Count != 0)
        {
            <h3>The following results were found for @ViewBag.searchString</h3>


            foreach (var search in @Model)
            {

                if (@search.type == "community")
                {
                    <div class="resource-element">
                        <a href="/Communities/CommunityPage/@search.CommunityId">
                            <span class="resource-type pull-right">Community</span>
                        </a>
                        <a href="/Communities/CommunityPage/@search.CommunityId"><h3>@search.CommunityName</h3></a>
                        <p>@search.Description</p>
                        <span class="">Type : @search.CommunityType</span><br />
                        <span class="">@search.CommunityCity, @search.CommunityState @search.CommunityZip</span><br />
                        <span class="">Population: @search.Population</span>
                        <br>
                    </div>
                }
                else
                {


                    <div class="resource-element">
                        <a href="@search.FilePath">
                            <span class="resource-type pull-right">Document</span>
                        </a>
                        <a href="@search.FilePath"><h3>@search.Title</h3></a>
                        <p>@search.Document_Description</p>
                        <span class="">@search.Date</span>
                        <br>
                        <span class="">@search.UpLoadedBy</span>
                        <br>
                    </div>
                }

            }

            @Html.PagedListPager(Model, pageNumber => Url.Action("Search", "Home", new { pageNumber }),
            new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true })

        }
        else
        {

            if (@Model.Count == 0)
            {
                <div class="resource-element">
                    <a href="#">
                        <span class="resource-type pull-right"></span>
                    </a>
                    <h3>No Results Found</h3>
                </div>

            }


        }
    }

}

【问题讨论】:

    标签: c# asp.net-mvc pagedlist


    【解决方案1】:

    它会回到您的GET 操作,因为Url.Action 呈现为&lt;a&gt; 标记。 &lt;a&gt; 链接总是执行 GET。没有办法解决这个问题(除了引入一些 javascript 来覆盖 &lt;a&gt; 链接点击事件)。事实上,这就是你可以解决这个问题的方法。

    1. 在表单中为“pageNumber”添加一个隐藏字段。
    2. 不使用Url.Action,只需使用onclick 事件呈现链接或按钮。
    3. 在该链接或按钮的onclick 事件中,增加隐藏的“pageNumber”值,然后通过 javascript 提交表单。

    希望对您有所帮助。

    【讨论】:

    • 谢谢...我能做到的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2020-08-04
    • 2018-10-23
    • 2011-02-22
    • 2021-03-16
    • 2016-11-20
    • 2019-01-25
    相关资源
    最近更新 更多