0

我无法在 Webgrid 中的页面之间保留过滤器(搜索)。与单击下一页时一样,搜索条件将丢失。有任何想法吗??

局部视图:

@model IEnumerable<NorthwindMVC_2.Models.GetOrdersResult>
@using NorthwindMVC_2.Models;

<input type="hidden" id="page" />

@{
ViewBag.Title = "_Grid";
Layout = null;
}

@using (Html.BeginForm(FormMethod.Get)) 
{  
<input type="search" name="SearchString" style="width:650px; margin-right:10px" placeholder="ID,  Ship Name, Ship Address, Required Date, Postal Code" id="txtSearch" />
<input type="submit" value="Search"/>
<input type="button" value="Clear" id="btnClear"/>

<script>


$("#btnClear").live("click", refreshGrid);

function refreshGrid() {

    var pageNumber = '@ViewBag.page';
 //$("#txtSearch").val("");

 $.ajax(
 {
     type: "GET",
     url: '@Url.Action("OrdersList", "Orders")',
 data: {
     searchText: $("#txtSearch").val(),
     page: pageNumber
 },
 dataType: "html",
 success: function (data) {
     $("#agrid").html(data);
 }
 })
 }

 </script>

<script type="text/javascript">

var PageNumber = '@ViewBag.page';
    $(document).ready(function page() {
        //alert(PageNumber);
    });


    //var page = '@ViewBag.page';



</script>

<style type="text/css">
    .webGrid { margin: 4px; border-collapse: collapse; width: auto; }
    .header { background-color: #d3dce0; font-weight: bold; color: whitesmoke; }
    .webGrid th, .webGrid td {border: 1px solid #C0C0C0; padding: 5px; }
    .alt { background-color: lightgrey; color: #000; }
    .footer { padding:10px; background-color:#d3dce0; color:black;}
    .birthdate { width:200px;}
    .id { width:50px;}
    .general { width: 120px;}
</style>


   var grid = new WebGrid(null,
            defaultSort: "",
            rowsPerPage: 10,
            canPage:true,
            ajaxUpdateContainerId:"grid",
            selectionFieldName: "SearchString",
            sortFieldName:"SortColumn",
            sortDirectionFieldName:"SortOrder");

<div style="display:none">
@grid.Bind(Model, autoSortAndPage: false, rowCount: ViewBag.tr);
</div>


 if (Model.Count() > 0)
 { 

 <div id="grid">

        @grid.GetHtml(
            tableStyle: "webGrid",
            headerStyle: "header",
            alternatingRowStyle: "alt",
            footerStyle: "footer",
            mode: WebGridPagerModes.All,
            numericLinksCount: 9,
            firstText: "First",
            lastText: "Last",
            previousText: "<",
            nextText: ">",
            columns: grid.Columns(
            grid.Column("OrderID", header:"ID", style:"id"),
            grid.Column("ShipName", "Name", style:"birthdate" ,  format: @<text>@Html.ActionLink((string)item.ShipName, "Edit", "Orders", new {  RowID = item.RowID, orderId = item.OrderID, page= (int) @ViewBag.page }, null)</text>),
            grid.Column("ShipAddress", header:"Address", style:"birthdate"),
            grid.Column("RequiredDate", header:"Required Date", style:"birthdate"),
            grid.Column("ShipPostalCode", header:"Postal Code", style:"general"),

              @*grid.Column("Action", format: @<text>
                <button class="edit-user display-mode" >Edit</button>
                <button class="save-user edit-mode"  >Save</button>
                <button class="cancel-user edit-mode" >Cancel</button>
                </text>,  style: "" , canSort: false)*@

            //grid.Column("Edit", format: (item) => Html.ActionLink("Edit", "Edit", "Orders", new { RowID = item.RowID, orderId = item.OrderID, page= (int) @ViewBag.page },  new  {  style = "color:#000" })),
            grid.Column("Delete", format: (item) => Html.ActionLink("Delete", "Delete", new { orderId = (int)item.OrderID }, new { onclick = "return confirm('Do you want to Delete this Order?')", style = "color:#000" })
        )
    )
)

</div>
  }
 else
 { 
<p>
No records found.</p>
}}      
<script type="text/javascript">

   var searchText = $("#txtSearch").val();
   $(function () {

       $('tfoot a').click(function () {
           var pageNumber = '@ViewBag.page';

          // $("#txtSearch").val("");
            $.ajax(
            { searchText : $("#txtSearch").val(),
                type: "GET",
                url: '@Url.Action("OrdersList", "Orders")',
                 data: {
                     searchText: $("#txtSearch").val(),
                     page: pageNumber
                 },
                 dataType: "html",
                 success: function (data) {
                     $("#agrid").html(data);
                 }
             })




       //    // when the user clicks on any of the pager links
       //    // try to extract the page number from the link and
       //    // set the value of the hidden field
       //    var SearchString = $("#txtSearch")
       //    var page = this.href.match(/page=([0-9])+/)[1];
       //    $('#page').val(page);

       //    alert(page);
       //    // submit the form so that the POST action is invoked
       //    // passing along the search criteria (Name and Year) along
       //    // with the page hidden field value to the Index action
       //    $('form').submit();

       //    // cancel the default action of the link which is to simply redirect
       //    // to the Index action using a GET verb.
       //    return false;
       });
   });

</script> 
4

1 回答 1

1

在您的 $('tfoot a').click(function ()... 中,它将使用您的 ajax 请求向操作方法发送“GET”请求( /?page=1 )。您必须处理寻呼机链接点击事件在这段代码中。

你可以做这样的事情

$('tfoot a').click(function () {

        var form = $('#yourformid');
        form.attr("action", this.href);
        $(this).attr("href", "#");
        form.submit();

    });

然后只有它会保留您的搜索条件模型

请看看这种方法

于 2014-10-13T08:24:05.900 回答