I have a strange behavior inside my WebGrid. now i am working on an asp.net mvc-4 web application . and i have the following action method to show a view which contain grid:-

public ActionResult ManageCustomAsset(int id, string filter = null, int page = 1, int? pageSize = null, string sort = "ID", string sortdir = "DESC")

           GridList<CustomAsset> gridrecords = repository.GetCustomAssetForGrid(id, filter, page, pageSize, sort, sortdir);

           if (Request.IsAjaxRequest())
               return PartialView("_CustomAsset2", gridrecords);
           return View("ManageCustomAsset2", gridrecords);

and here is the main view:-

<h2>Custom Asset</h2>
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">

@section Scripts {


    <script src="~/Scripts/js/bootstrap-modal.js"></script>

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


                cache: false,
                type: "Get",
                url: '@Url.Action("ManageCustomAsset", "Customer")',

                data: { id2:"@Model.CustomAsset.CustomerID.Value.ToString()", pageSize: $('#FilterSize1').val(), page: "1", sort: $('#currentsort').val(), sortdir: $('#currentsortdir').val() },

                success: successFunc,
                error: errorFunc

            function successFunc(data, status) {



            function errorFunc() {


and the _CustomAsset2 partial view:-

@model  T.ViewModels.GridList<T.Models.CustomAsset>

  <div class="row-fluid sortable">      
                <div class="box span12">
                    <div class="box-header well" data-original-title>
                        <h2><i class="icon-th"></i> Custom Assets </h2>                    

                        <div class="box-icon">

                    <div class="box-content">
<div><span class="b"> There are @Model.TotalRecords items.</span></div><br />
<input type="hidden" value="@Model.currentsort" id="currentsort" /> 
<input type="hidden" value="@Model.currentsortdir" id="currentsortdir" />  
        var gridcolumns = new List<WebGridColumn>();
         gridcolumns.Add(new WebGridColumn()
            //ColumnName = "Actions",
            CanSort = false,
            Format =


 "DeleteCustomerCustomAsset", "Customer",
new { id = item.ID },

new AjaxOptions
{ Confirm = "Are You sure You want to delete (" + item.CustomAssetType.Name.ToString() +  ")",
    HttpMethod = "Post",

    OnSuccess = "deletionconfirmation",
    OnFailure = "deletionerror"
         //gridcolumns.Add(new WebGridColumn()
         //    ColumnName = "CustomerName",
         //    Header = Html.DisplayNameFor(model => model.Content.FirstOrDefault().CustomerName).ToString(),
         //    CanSort = false
         gridcolumns.Add(new WebGridColumn()
             ColumnName = "CustomAssetType.Name",
             Header = Html.DisplayNameFor(model => model.Content.FirstOrDefault().CustomAssetType.Name).ToString(),
             CanSort = true

         gridcolumns.Add(new WebGridColumn()
             ColumnName = "Description",
             Header = Html.DisplayNameFor(model => model.Content.FirstOrDefault().Description).ToString(),
             CanSort = true
         gridcolumns.Add(new WebGridColumn()
             ColumnName = "Quantity",
             Header = Html.DisplayNameFor(model => model.Content.FirstOrDefault().Quantity).ToString(),
             CanSort = true

         gridcolumns.Add(new WebGridColumn()
             ColumnName = "Rack.Technology.Tag",
             Header = Html.DisplayNameFor(model => model.Content.FirstOrDefault().RackID).ToString(),
             CanSort = true,
             Format = 
             @Html.ActionLink((string)item.Rack.Technology.Tag.ToString(), "Details", "Rack", new { id = item.RackID},null)
         var grid = new WebGrid(
                        canPage: true,
                        rowsPerPage: Model.PageSize,
                        canSort: true,
                        ajaxUpdateContainerId: "grid");

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

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


as specific inside my code , i am doing an initial sort using the ID, but i am not showing the ID inside the grid columns. now when i first render the web grid (using non-ajax request) every thing will work well. but if i call the action method using Ajax call (by changing the filter size dropdown list) , then i will get the following exception , inside the grid:-

Column "Id" does not exsits

enter image description here

so i am not sure why the grid raised this error when the request is ajax ? i mean the sort is being held inside the database level, not inside the view ..so why the grid need to know the current sort ? and why this error will be raised only when i am calling the action method using ajax call ? Also if i change my action method as follow , by replacing the ID with let say CustomAssetType.Name the error will be removed:-

public ActionResult ManageCustomAsset(int id, string filter = null, int page = 1, int? pageSize = null, string sort = "CustomAssetType.Name", string sortdir = "DESC")

but in my case i want the initial sort to be based on the ID and i do not want to show the ID inside the Grid ?? so is this possible ?

here is my repository method:-

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

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

            records.TotalRecords = GetCustomAssetForGridCount(filter, id);

            records.CurrentPage = page;
            records.PageSize = pageSize.Value;
            records.currentsort = sort;

            records.currentsortdir = sortdir;
            records.Content = t.CustomAssets.Include(a => a.Rack.Technology).Where(x =>
                            x.CustomerID == id)
                    .OrderBy(sort + " " + sortdir)
                    .Skip((page - 1) * pageSize.Value)

            records.CustomAsset.CustomerID = id;
            records.CustomAsset.CustomerName = GetCustomerNameByID(id);
            return records;


Related posts

Recent Viewed