Home > OS >  Make Kendo ASP.NET MVC inCell Editable based on Viewbag condition
Make Kendo ASP.NET MVC inCell Editable based on Viewbag condition

Time:03-31

Is it possible to make a Kendo MVC grid .Editable() based on a function that allows editing ONLY if you have a certain Viewbag?

I have a viewbag that is Viewbag.DisplayButton. That viewbag is only 'true' if you have a dev role (so non-devs cannot edit anything). How can I make this work with .Editable() so that you can only edit cells if you have that viewbag?

Currently if I set Editable(true) then anyone (devs, customers, literally anyone) can edit the cell. If I set it to Editable(false) then no one, including devs, can edit it. So I need a function that does it only if you have that specific viewbag.

CodePudding user response:

Use a Razor code block for this. You can assign the Grid definition to a variable, then execute the conditional logic and add the additional configurations, if any. Finally call the Render method. Here is an example:

<h3>Some content</h3>
@{
  var isAdmin = true;

  var grid = (Html.Kendo().Grid<MyModel>()
             .Name("grid")
             .Columns(columns =>
             {
                 columns.Bound(p => p.MyModelID).Filterable(false);
                 columns.Bound(p => p.SomeModelProperty);
             })
             .Pageable()
             .Scrollable()
             .DataSource(dataSource => dataSource
                 .Ajax()
                 .PageSize(20)
                 .Model(m=>m.Id("MyModelID"))
                 .Read(read => read.Action("Read", "Grid"))
             )
         );

  if (User.IsInRole("Admin"))
  {
      grid.Editable(e=>e.Mode(GridEditMode.InCell));
  }

  grid.Render();
}
<h3>Some other content</h3>
  • Related