asp.net mvc局部视图正在缓存我的模型值

时间:2015-03-17 14:43:49

标签: c# asp.net asp.net-mvc asp.net-mvc-5 partial-views

我正在开发一个asp.net mvc Web应用程序。我有一个WebGrid,我在其中添加了一个页面大小的下拉列表,使用户可以选择他们喜欢每页有多少条记录。

Action方法是: -

 [OutputCache(CacheProfile = "NoCache")]
 public ActionResult Disposed(string filter = null, int page = 1, int? pageSize = null, string sort = "Technology.Tag", string sortdir = "ASC")
        {
            GridList<DisposedResources> gridrecords = repository.GetDisposedResourcesForGrid(filter, page, pageSize, sort, sortdir, "rack");
            ViewBag.PagedSizeOptions = new PageOptions().FilterOptions;
            if (Request.IsAjaxRequest())
            {

                return PartialView("_disposed", gridrecords);
            }
            return View("Disposed", gridrecords);
        }

这是存储库方法: -

  public GridList<DisposedResources> GetDisposedResourcesForGrid(string filter, int page, int? pageSize, string sort, string sortdir, string resourcetype)
        {
            if (!pageSize.HasValue)
            {
                pageSize = Int32.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings["TechPageSize"]);
            }

            var records = new GridList<DisposedResources>();
            records.currentfilter = filter;

            records.TotalRecords = GetDisposedResourcesForGridCount(filter, resourcetype);
            records.hasNetworkInfo = false;
            records.hasSystemInfo = false;
            records.CurrentPage = page;
            records.PageSize = pageSize.Value;
            records.currentsort = sort;
            records.currentsortdir = sortdir;
            records.Content = tms.DisposedResources.Include(a=>a.Technology).Where(x => (filter == null ||
                            (x.Technology.Tag.ToLower().StartsWith(filter.ToLower()))

                          ) && x.ResourceType.ToLower() == resourcetype.ToLower())
                    .OrderBy(sort + " " + sortdir)
                    .Skip((page - 1) * pageSize.Value)
                    .Take(pageSize.Value).ToList();
            return records;

        }

Disposed视图是: -

@model  S.ViewModels.GridList<S.Models.DisposedResources>




Show   @Html.DropDownList("FilterSize", new SelectList(ViewBag.PagedSizeOptions, "Value", "Text", ViewBag.pagesize ), new { @id= "FilterSize1",@class="SmallDropDown3"}) <span class="hidden-phone">per page.</span> 

<div id="disposed">

@Html.Partial( "_disposed",Model)

</div>

@section Scripts {



    <script type="text/javascript">
        $("body").on('change', '#FilterSize1', function () {

            //$(SizeProgressSort).show();
            $.ajaxSetup({ cache: false });
            $.ajax({
                type: "Get",
                url: '@Url.Action("Disposed")',

                data: { pageSize: $('#FilterSize1').val(), page: "1", sort: $('#currentsort').val(), sortdir: $('#currentsortdir').val() },

                success: successFunc,
                error: errorFunc
            });

            function successFunc(data, status) {

                $('#disposed').html(data);


            }

            function errorFunc() {
                alert('error');
            }
        });

    </script>
}

和_disposed部分视图是: -

@model  S.ViewModels.GridList<S.Models.DisposedResources>



        var gridcolumns = new List<WebGridColumn>();
        gridcolumns.Add(new WebGridColumn()
        {
            ColumnName = "Technology.Tag",
            Header = Html.DisplayNameFor(model => model.Content.FirstOrDefault().Technology.Tag).ToString(),
            CanSort = true
        });
       //code goes here...
            var grid = new WebGrid(
                        canPage: true,
                        rowsPerPage: Model.PageSize,
                        canSort: true,
                        ajaxUpdateContainerId: "grid");

            grid.Bind(Model.Content, rowCount: Model.TotalRecords, autoSortAndPage: false);
            grid.Pager(WebGridPagerModes.All);

            @grid.GetHtml(htmlAttributes: new { id = "grid" },   // id for ajaxUpdateContainerId parameter
            fillEmptyRows: false,
            tableStyle: "table table-bordered table-hover",
            mode: WebGridPagerModes.All,
            columns: gridcolumns

            );
            }
             </div></div></div>
<input type="hidden" value="@Model.currentsort" id="currentsort" /> @Model.currentsort
<input type="hidden" value="@Model.currentsortdir" id="currentsortdir" /> @Model.currentsortdir
我面临的问题是两个参数;作为javascript的一部分传递的currentsort + currentsortdir将不会被更改,并且如果用户选择了chnage页面大小下拉列表,则用户将失去当前的排序顺序。所以任何人都可以建议问题是什么,现在即使我选择显示这两个值: -

Model.currentsort

&

Model.currentsortdir

它们将始终具有defualt值,尽管这些值正在存储库方法中更改...但似乎部分视图以某种方式缓存这两个参数的旧值?

2 个答案:

答案 0 :(得分:1)

ModelState可能会覆盖您在模型中更改的值。在您的操作方法中调用ModelState.Clear(),您应该会看到更改后的值。

答案 1 :(得分:0)

我知道你已经通过ajaxSetup完成了缓存设置,但是尝试在你的脚本中放入cache:false,看看是否会产生影响。

     $.ajax({
            cache: false
            type: "Get",
            url: '@Url.Action("Disposed")',
           --------