Using Kendo Grid in PartialView

强颜欢笑 提交于 2019-12-10 00:10:51

问题


I have two <section> in the Index view of my MVC application and I want to render two partial views in these sections. There is no problem rendering a Kendo grid to one Index. However, in order to render data on Kendo Grid, could I use the two methods returning Json in the controller as shown below. Could you give me an example how to achieve this?

Controller:

public ActionResult Index()
{
    return View();
}

public ActionResult Issues_Read([DataSourceRequest]DataSourceRequest request)
{
    IQueryable<Issue> issues = db.Issues;
    DataSourceResult result = issues.ToDataSourceResult(request, c => new IssueViewModel 
    {
        ID = c.ID,
        ProjectID = c.ProjectID
    });

    return Json(result);
}


View:

@(Html.Kendo().Grid<IssueViewModel>()
  .Name("grid")
  .Columns(columns =>
  {
      columns.Bound(c => c.ProjectID);
      columns.Command(command => { command.Edit(); command.Destroy(); }).Width(180);
  })
  .ColumnMenu()
  .Editable(editable => editable.Mode(GridEditMode.PopUp))
  .Pageable()
  .Navigatable()    
  .DataSource(dataSource => dataSource
      .Ajax()
      .Model(model => model.Id(p => p.ID))
      .Read(read => read.Action("Issues_Read", "Issue"))
      .Create(create => create.Action("Issues_Create", "Issue" ))
      .Update(update => update.Action("Issues_Update", "Issue"))
      .Destroy(destroy => destroy.Action("Issues_Destroy", "Issue"))
  )
)

回答1:


In order to use the same partial view multiple times, grid ID should be unique so passing the ID in partial view data is one possible solution. In your case Partial view first call:

@Html.Partial("grid", new ViewDataDictionary { { "id", "grid1" }})

Partial view second call:

@Html.Partial("grid", new ViewDataDictionary { { "id", "grid2" }})

Partial view content:

@(Html.Kendo().Grid<IssueViewModel>()
  .Name(@ViewData["id"].ToString())
...


来源:https://stackoverflow.com/questions/30397741/using-kendo-grid-in-partialview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!