将控制值作为html.Pagedlist参数传递 [英] To pass control values as html.Pagedlist parameters

查看:55
本文介绍了将控制值作为html.Pagedlist参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using paged list to display a list of values. The display works fine. I use the provided Unobtrusive AJAX to get the data for the other pages.

这是我的分页控件的外观.

This is how my paged control looks.

@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页面"列表一起传递.

Now I have a textbox and a drop down box in the page and I want to pass values present in those along with the Html Paged list.

@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将控件的当前值作为参数传递给action方法?

So how do I go about passing the current value of the controls to the action method as parameters using html.PagedList?

推荐答案

您需要在客户端上处理此问题,因为Razor不了解用户选择了什么.请记住:页面呈现后,Razor不再是该过程的一部分.

You'll need to handle this on the client, as Razor is unaware of what the user has selected. Remember: once the page is rendered, Razor is no longer part of the process.

这是我在最近的项目中使用的内容:

Here's what I used on a recent project:

<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>

在载入文档时,请注意以下事项

With the following on doc load,

$('#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();
    }
});

实用程序功能(不是我的,虽然不记得我在哪里找到的),

The utility function (not mine, though can't remember where I found it) is as such,

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

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

So the trick is to intercept the user's click of the paging control, use hidden fields to submit whatever values you want, and then read the values on the controller. In this case, I have a hidden field called selectedOption; add others as you need them and populate them in JavaScript with the values the user has provided. Then do a submit on the form.

请注意,此处的表单方法是POST.您还需要将 FormCollection 传递到您的操作中,以便您阅读张贴的隐藏字段.

Note that the form's method is a POST here. You'll also want to pass in the FormCollection into your action so you can read the hidden fields posted.

这篇关于将控制值作为html.Pagedlist参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆