簡體   English   中英

我可以將@ Html.PagedListPage與@ url.Action帖子一起使用嗎

[英]Can I use a @Html.PagedListPage with a @url.Action post

我想要做的是有一個輸入框,用作“搜索字符串”,它是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>

            }


        }
    }

}

由於Url.Action呈現為<a>標記,因此可以返回到GET操作。 <a>鏈接始終執行GET 除此以外,沒有其他方法(除了引入一些JavaScript來覆蓋<a>鏈接click事件外)。 實際上,這就是解決該問題的方法。

  1. 在表單上為“ pageNumber”添加一個隱藏字段。
  2. 而不是使用Url.Action ,只需渲染帶有onclick事件的鏈接或按鈕。
  3. 在該鏈接或按鈕的onclick事件中,增加隱藏的“ pageNumber”值,然后通過javascript提交表單。

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM