加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将控制值作为html.Pagedlist参数传递

发布时间:2020-12-16 01:27:07 所属栏目:百科 来源:网络整理
导读:我正在使用分页列表来显示值列表.显示效果很好.我使用提供的Unobtrusive AJAX来获取其他页面的数据. 这是我的分页控件的外观. @Html.PagedListPager(Model.CountryList,page = Url.Action("GetCountries","Dashboard",new {page}),PagedListRenderOptions.En
我正在使用分页列表来显示值列表.显示效果很好.我使用提供的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,new {country = $("#SearchCountryName").val(),continent = $("#Continent").val(),page}),OnFailure = "onAjaxFailure"
}))

但这不起作用.

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

解决方法

您需要在客户端上处理此问题,因为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>

在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();
    }
});

效用函数(不是我的,虽然不记得我发现它的位置)是这样的,

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读