使用局部视图的kendo标签中的kendo网格

时间:2013-04-25 04:37:15

标签: c# razor entity-framework-4 kendo-ui

出于某种原因,我可以获得第二个选项卡(产品详细信息)来绑定网格中的记录,尽管GetAllProductList返回记录。请指教,谢谢

index.cshtml HOME / Index.cshtml

@(Html.Kendo().TabStrip()
    .Name("tabstrip")
    .Items(items =>
    {
        items.Add().Text("Search")
            .LoadContentFrom("Index", "ProductDetails")
            .Selected(true);



        items.Add().Text("Product Details")
       .LoadContentFrom("_ProductData", "ProductDetails")



    })
)

产品详细/ Index.cshtml

@(Html.Kendo()。网格()

        .Name("BookGrid")
        .HtmlAttributes(new { @Style = "align:center; font-size:10px; width:950px" })
        .Columns(columns =>
        {

            columns.Bound(p => p.BookId).Width(95);
            columns.Bound(p => p.Name).Width(120);


        })

        .ToolBar(toolbar => toolbar.Create())
        .Sortable()
            //.Pageable()
        .Pageable(paging => paging
            .Input(false)
            .Numeric(true)

            .PreviousNext(true)
            .PageSizes(new int[] { 5, 10, 25, 50 })
            .Refresh(false)

        )
        .Selectable()
        .Scrollable()
        .ColumnMenu(c => c.Columns(false))
        .DataSource(dataSource => dataSource

            .Ajax()//bind with Ajax instead server bind
            .PageSize(10)
            .ServerOperation(true)
                .Model(model =>
                {
                    model.Id(p => p.BookId);

                })
                                     .Sort(sort => sort
                                  .Add(x => x.Name).Descending())

                        .Read(read => read.Action("GetBookData", "ProductDetails").Type(HttpVerbs.Get))


            )



    )

ProductDetails / _ProductData.chtml(部分页面)

@(Html.Kendo().Grid<HH.PrductModel>()

        .Name("ProductGrid")
        .HtmlAttributes(new { @Style = "align:center; font-size:10px; width:950px" })
        .Columns(columns =>
        {

            columns.Bound(p => p.ProductId).Width(95);
            columns.Bound(p => p.Name).Width(120);


        })

        .ToolBar(toolbar => toolbar.Create())
        .Sortable()
            //.Pageable()
        .Pageable(paging => paging
            .Input(false)
            .Numeric(true)

            .PreviousNext(true)
            .PageSizes(new int[] { 5, 10, 25, 50 })
            .Refresh(false)

        )
        .Selectable()
        .Scrollable()
        .ColumnMenu(c => c.Columns(false))
        .DataSource(dataSource => dataSource

            .Ajax()//bind with Ajax instead server bind
            .PageSize(10)
            .ServerOperation(true)
                .Model(model =>
                {
                    model.Id(p => p.ProductId);

                })
                                     .Sort(sort => sort
                                  .Add(x => x.Name).Descending())

                        .Read(read => read.Action("GetProductData", "ProductDetails").Type(HttpVerbs.Get))


            )



    )

**ProductDetailscontroller**

public ActionResult Index()

{ 
   return View();
} 

///Display for tab 1
public ActionResult GetBookData ([DataSourceRequest] DataSourceRequest request)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
            return Json(GetAllBookList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}

 private static IEnumerable<BookModel> GetAllBookList()
        {
            using (var dc = new HHEntities())
            {
                var result = (from a in dc.Books

                              select new BookModel
                              {
                                  BookId= a.BookId,
                                  Name= a.Name



                              });
                return result.Distinct().ToList();
            }


        }


///Display for tab 2

public ActionResult GetProductData([DataSourceRequest] DataSourceRequest request)
{
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            return Json(GetAllProductList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

以下内容将返回记录,但由于某种原因,它将绑定到“产品详细信息”选项卡中的网格

       /// <summary>

        /// </summary>
        /// <returns></returns>
        private static IEnumerable<ProductModel> GetAllProductList()
        {
            using (var dc = new HHEntities())
            {
                var result = (from a in dc.Products

                              select new ProductModel
                              {
                                  ProductId= a.ProductId,
                                  Name= a.Name,
                                  Description= a.Description,
                                  ExpiryDate= a.ExpiryDate


                              });
                return result.Distinct().ToList();
            }


        }

    public ActionResult __ProductData()
  { return PartialView();}

1 个答案:

答案 0 :(得分:1)

我怀疑问题是这两个网格都使用相同的HTML id属性(通过Name()方法设置)进行渲染。您需要为您的网格提供唯一的名称。例如,您可以在动作方法中的ViewData中放置一些索引,以呈现包含网格的局部视图。然后在局部视图中使用该索引,使网格名称唯一:

@(Html.Kendo().Grid<HH.PrductModel>()
      .Name("Product" + ViewData["index"])