视图中的参数未进入控制器操作方法

时间:2012-02-09 18:38:20

标签: asp.net-mvc-3 pagedlist

我正在我的一个视图(ASP.NET MVC 3 Razor)中实现Troy Goode的PagedList。我遇到的挑战是,当我点击页码链接时,请求被路由到我的HttpGet方法,该方法只返回空页面(准备输入)。

我的视图模型:

public class SearchViewModel
{
    public SelectList IndustrySelectList { get; set; }
    public IPagedList<KeyValuePair<string, SearchResult>> SearchResults { get; set; }
    public PagingInfo PagingInfo { get; set; }
}

控制器:

    [HttpGet]
    public ViewResult Search(string searchTerm = "")
    {
        SearchViewModel vm = new SearchViewModel
        {
            IndustrySelectList = new SelectList(_Industries.AsEnumerable(), "IndustryId", "IndustryName"),
            PagingInfo = new PagingInfo
            {
                CurrentPage = 1,
                ItemsPerPage = 25,
                TotalItems = 0
            }
        };

        return View(vm);
    }

    [HttpPost]
    public ActionResult Search(string[] industries, string searchTerm = "", int page = 1)
    {
        SearchViewModel vm = null;

        _url = "http://localhost/MasterNode/masternode.cgi?zoom_query={" + searchTerm + "}&zoom_xml=1&zoom_page={startPage?}&zoom_per_page=1000";
        StringBuilder sb = new StringBuilder();
        int pageSize = 5;

        if (string.IsNullOrEmpty(searchTerm))
        {
            vm = new SearchViewModel
            {
                IndustrySelectList = new SelectList(_Industries.AsEnumerable(), "IndustryId", "IndustryName")
            };
        }
        else
        {
            _request = new SearchRequest(SearchRequest.EnvironmentTypes.Development, "", _url, searchTerm, SearchRequest.SearchType.AllWords, 1000);
            sb.Append(GetResults(_url));
            _results = new Dictionary<string, SearchResult>();
            ParseResults(sb);
            GetDetailInformationForResults(searchTerm);

            vm = new SearchViewModel
            {
                IndustrySelectList = new SelectList(_Industries.AsEnumerable(), "IndustryId", "IndustryName"),
                SearchResults = _results.ToList<KeyValuePair<string, SearchResult>>().ToPagedList(1, 25),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = pageSize,
                    TotalItems = _results.Count()
                }
            };
        }
        return View(vm);
    }

查看:

@model MultiView.OmniGuide.ViewModels.SearchViewModel
@using MultiView.OmniGuide.HtmlHelpers
@using PagedList
@using PagedList.Mvc
@{
    ViewBag.Title = "Search";
}
<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />

@using (Html.BeginForm("Search", "Home")) 
{
    @Html.HiddenFor(c => c.IndustrySelectList)
    @Html.HiddenFor(c => c.PagingInfo)
    @Html.HiddenFor(c => c.SearchResults)
    <table width="70%">
        <tr>
            <td colspan="2" style="background: #fff">
                <input id="searchTerm" name="searchTerm" type="text" class="SearchBox" style="width: 450px" />
                <input type="submit" class="SearchButton" value=" " />
            </td>
        </tr>
        <tr align="left">
            <td align="left" style="background: #fff">
                @Html.ActionLink("MultiView corporate site", "Search")
            </td>
        </tr>
        <tr>
            <td colspan="1" align="center" style="width: 450px">
                @{
                    Html.Telerik().PanelBar()
                        .Name("searchPanel")
                        .Items(title =>
                        {
                            title.Add()
                                .Text("Filter by Industry")
                                .Content(() =>
                                {
                                    @Html.RenderPartial("_Industry", @Model);
                                });
                        })
                        .Render();       
                }
            </td>
        </tr>
        <tr><td colspan="2"></td></tr>
    </table>
    <br />
    if (Model.SearchResults != null)
    {
        <table width="70%">
            <tr>
                <th>
                    Company Image
                </th>
                <th class="tableHeader">
                    Company Name Here
                </th>
                <th class="tableHeader">
                    Website
                </th>
            </tr>     
        @foreach (KeyValuePair<string, MultiView.OmniGuide.Models.SearchResult> itm in Model.SearchResults)
        { 
            <tr>
                <td align="left" style="width: 15%">
                    @itm.Value.DetailedInfo.LogoURL 
                </td>
                <td align="left" style="width: 60%">
                    <p style="text-align: left">
                     @itm.Value.DetailedInfo.DescriptionAbbreviated
                     <br />
                     </p>
                    @Html.AnchorLink(itm.Value.FoundURL, itm.Value.FoundURL)
                </td>
                <td style="width: 25%">
                    @itm.Value.FoundURL
                </td>
            </tr>
        }
        </table>    
        @Html.PagedListPager((IPagedList)Model.SearchResults, page => Url.Action("Search", "Home", new { page }))
    }
}

当在输入框中提供文本并单击该按钮时,请求被路由到HttpPost方法。在查看request.form值时,存在所有预期数据但是分页信息。

?HttpContext.Request.Form.AllKeys  
{string[5]}
    [0]: "IndustrySelectList"
    [1]: "PagingInfo"
    [2]: "SearchResults"
    [3]: "searchTerm"
    [4]: "industries"

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

点击您提交表单的按钮,这就是httppost的原因。下一页链接正确访问httpget,但您没有将任何信息传递给它,以便它知道get的内容。获取需要其他信息,例如您想要的页面。

答案 1 :(得分:0)

页码链接会触发GET请求,因此您需要确保GET操作也可以处理完整搜索,因此需要获取页码和行业array - 当这些参数不可用时使用默认值。

e.g。

[HttpGet]
public ViewResult Search(string searchTerm = "", int page = 1, 
     string industries = "")
{
    //.....
}

您需要像这样修改寻呼机链接,以便将行业传递给获取操作。

@Html.PagedListPager((IPagedList)Model.SearchResults, page => Url.Action("Search", "Home", new { page, industries = string.Join(",", Model.IndustrySelectList.Where( x => x.Selected).Select( x => x.Text)) }))

我不清楚你的代码中的帖子操作是从string[] industries获得的,或者它正在用它做什么,但是你需要一些方法将这一点传递给你的get动作,可能是一个逗号分隔的字符串。我提供的示例假设您是从viewmodel

上的选择列表中获取的