【问题标题】:To pass control values as html.Pagedlist parameters将控件值作为 html.Pagedlist 参数传递
【发布时间】:2018-11-16 11:31:30
【问题描述】:

我正在使用分页列表来显示值列表。显示器工作正常。我使用提供的 Unobtrusive AJAX 来获取其他页面的数据。

这就是我的分页控件的外观。

@Html.PagedListPager(Model.CountryList, page => Url.Action("GetCountries", "Dashboard", new {page}), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast, new AjaxOptions()
{
    HttpMethod = "POST",
    UpdateTargetId = "panel1",
    OnSuccess = "onAjaxSuccess",
    OnFailure = "onAjaxFailure"
})) 

这很好用。

现在我在页面中有一个文本框和一个下拉框,我想将其中的值与 Html 分页列表一起传递。

@Html.TextBox("SearchCountryName", new { name = "SearchCountryName", @class = "form-control", placeholder = "Search Country" })

@Html.DropdownList("Continent", ContinentList)

我的控制器代码是:

public PartialViewResult GetDashboardBody(string country,string continent,int? page)
{

}

我想做类似的事情

@Html.PagedListPager(Model.CountryList, page => Url.Action("GetCountries", "Dashboard", new {country = $("#SearchCountryName").val(),continent = $("#Continent").val(),page}), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(PagedListRenderOptions.ClassicPlusFirstAndLast, new AjaxOptions()
{
    HttpMethod = "POST",
    UpdateTargetId = "panel1",
    OnSuccess = "onAjaxSuccess",
    OnFailure = "onAjaxFailure"
})) 

但这不起作用。

那么如何使用 html.PagedList 将控件的当前值作为参数传递给操作方法?

【问题讨论】:

  • 您是否尝试将下拉列表中的选定项目传递到下一页?如果是这样,您将不得不忘记 Razor 并使用一些 jQuery 来检测页面 #click 并手动处理它。如果你想传递更复杂的对象,你需要一个路由值字典。
  • 这毫无意义。如果您更改搜索/过滤器值,那么您应该在单独的 ajax 调用中执行此操作(以便显示新结果的第一页),而不是尝试更改现有按钮的 url。
  • @theFallenOne 我明白了,但您能否发布您自己的答案,看看您做了什么。我有同样的问题。

标签: c# jquery ajax asp.net-mvc pagedlist


【解决方案1】:

您需要在客户端处理此问题,因为 Razor 不知道用户选择了什么。请记住:一旦页面被渲染,Razor 就不再是该过程的一部分。

这是我在最近的一个项目中使用的:

<div class="text-center" id="pagingControl">
    <input type="hidden" id="selectedOption" name="selectedOption" />
    @Html.PagedListPager(Model.Logs, page => Url.Action("Index", "Log",
        new { page = page }),
        PagedListRenderOptions.ClassicPlusFirstAndLast)
</div>

在文档加载时,

$('#pagingControl').find('a[href]').on('click', function (e) {
        e.preventDefault();
        $("#selectedOption").val("something"); // When you submit, you can read this field on the controller and pass it on
        var page = utilities.getQueryStringValue(this, 0).replace('page=','');
        $('#AdvancedSearchForm').submit();
    }
});

效用函数(不是我的,虽然不记得我在哪里找到的)就是这样,

getQueryStringValue: function (anchor, index) {
    var queryString = $(anchor).attr('href').split('?')[1];
    var values = queryString.split('&');
    return values[index];
}

所以诀窍是拦截用户对分页控件的点击,使用隐藏字段提交你想要的任何值,然后读取控制器上的值。在这种情况下,我有一个名为 selectedOption 的隐藏字段;根据需要添加其他人,并使用用户提供的值在 JavaScript 中填充它们。然后在表单上提交。

请注意,这里的表单方法是 POST。您还需要将FormCollection 传递到您的操作中,以便阅读发布的隐藏字段。

【讨论】:

  • 我没有使用函数 getQueryStringValue,而是使用普通的 AJAX 帖子并将其他参数添加为数据的一部分,这对我有用。谢谢。
  • @Alex 我在这里遇到了同样的问题stackoverflow.com/questions/64309882/… 我想知道你是否看一下。我在那里使用 ajax,所以这让它变得有些不同。
  • @AliEshghi,看看我对你问题的回答
猜你喜欢
  • 2021-01-26
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 2011-11-02
相关资源
最近更新 更多