1

I am loading some ids to a ViewBag to use it's data in the related View.

var userAccountList =serviceResponse.RoleList.Select(x => x.RoleId).ToList();
ViewBag.UserRoles = userAccountList;

this result is a List<string>.

In the View a Kendo Grid retrieves data from json.

@(Html.Kendo().Grid<RoleModel>().Name("KendoGrid").Scrollable().Columns(columns =>
 {
        columns.Bound(x => x.Id)
            .HtmlAttributes(new { style = "text-align:center;"})
            .ClientTemplate("# if ( Id != '3'){
                #<a class='k-button' href=\"/Role/Delete/#= Id #\"><span class='k-icon k-delete' style='pointer-events: none;'></span>Delete</a># }#")
            .Width(85).Sortable(false)
            .Hidden(!((BaseController)this.ViewContext.Controller).UserHasPermission(PermissionType.RoleDelete));
        columns.Bound(x => x.Name);

....
 }

When I use static values such as '3' I could prevent to show a tag that was used as delete button. How can I use ViewBag.UserRoles in this conditional if the list contains Id that's bound to column?

Anastasios Selmani
  • 3,579
  • 3
  • 32
  • 48
Murat Seker
  • 890
  • 2
  • 14
  • 26
  • 1
    What I do is pass the values from the controller to the view's model. Then add a hidden input (HiddenFor). Now you can create a jquery function like GetId that returns `$('#myHidden').val()`. Then `# if ( Id != GetId())...` – Steve Greene Jul 17 '18 at 13:39
  • thx. the way that you say exactly have done what I want. – Murat Seker Aug 05 '18 at 11:43

1 Answers1

1

Generally in order to use Razor you would have to do something like that

.ClientTemplate("# if ( Id != "'+ @ViewBag.UserRole +'")

If we were talking about one string value. Now for your case you could use Template instead of ClientTemplate with something like this

.Template(
    @<text>
         @if (ViewBag.UserRoles.Any(u => u == item.Id))
         {
            ... add your link
         }
   </text>

A couple more examples you can find in this post. Most probably it has documentation too by I couldn't find what I was looking for at the moment.

Anastasios Selmani
  • 3,579
  • 3
  • 32
  • 48
  • calling a js function have done it. I have a little complicated client and header template so I did not to prefer such a way like this. I will try this later for a simplier sample. thx for explanation. – Murat Seker Aug 05 '18 at 11:46