简体   繁体   中英

Grid.mvc use in partial view

I have a problem about use Grid.mvc in Partial View. When I use grid.mvc in View, it working normal but when I use in partial view, grid.mvc can't paging, filter... Have any idea? Thanks you very much! This is my code:


  
 
  
  
  
<!-- Index.html -->
    @{
        ViewBag.Title = "ABC";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
   <div class="row">          
        <div class="col-xs-9 col-lg-10">
            <div class="box box-primary">
                <div class="box-header with-border"></div>
                <div class="box box-body">
                    <div class="row">
                        <div class="col-xs-12 col-lg-12">
                            <div id="products"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


<script>
    $(function() {
        $.ajax({
            url: '@Url.Action("GetProducts", "Equipment")',
            dataType: "html",
            type: "GET",
            cache: false,
            contentType: 'application/html; charset=utf-8',
            success: function (data) {
                $("#products").html(data);
            },
            error: function (xhr) {
                alert(xhr);
            }
        });
    });
</script>


<!--Partial view-->

@using GridMvc.Html

<script src="@Url.Content("~/Content/Scripts/gridmvc.min.js")"></script>
<script src="@Url.Content("~/Content/Scripts/jquery-1.10.2.min.js")"></script>
@Html.Grid((IEnumerable<Model.ViewModels.EquipmentModel>)ViewBag.ListEquipment).Named("ast").Columns(columns =>
                       {
                           columns.Add(c => c.Serial_No).Titled("Số serial").Filterable(true).SetWidth(100).Sortable(true);
                           columns.Add(c => c.Name).Titled("Tên thiết bị").Filterable(true).SetWidth(250).Sortable(true);
                       }).WithPaging(10).Sortable(true)
<script src="~/Content/Scripts/gridmvc.js"></script>

<!-- Controller -->

  public class EquipmentController : BaseController
    {
        string strError = "";

        // GET: Equipment
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetProducts(string ID)
        {       
            var model = getEquipmentByID(Convert.ToInt16(ID));  //Get data
            ViewBag.ListEquipment = model;
            return PartialView("GetProducts", model);
        }
}

So you'll just need to re-instantiate the grid after its called, use $(".grid-mvc").gridmvc(); from your "success" method in ajax

I used ajax with my grid in a partial view. For the paging, I made a function in main view and edited the partial view _GridPager.cshtml to call my function on click. The function again calls a Ajax function which will just load the right page.

$.ajax({
                    type: "POST",
                    url: "http://ABC?grid-page=" + myPage,
                    data: mydata,
                    success: successFunc,
                    error: errorFunc
});

by the way the above Grid instantiation solved my issue with the CSS on the paging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM