我可以在@ url.Action帖子中使用@ Html.PagedListPage吗?

时间:2017-04-10 20:23:16

标签: c# asp.net-mvc pagedlist

我想要做的是我有一个输入框,用作"搜索字符串",它是搜索操作的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>

            }


        }
    }

}

1 个答案:

答案 0 :(得分:1)

它将返回到您的GET操作,因为Url.Action呈现为<a>标记。 <a>链接始终执行GET。没有办法(除了引入一些javascript来覆盖<a>链接点击事件)。事实上,这就是你如何解决这个问题。

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