locked
Conditionally show icon in WebGrid problem with int to bool RRS feed

  • Question

  • User-453926175 posted
    format: @<text>@(item.RoleId ?
                Html.Raw("<i class=\"icon-star action\"></i>") :
                Html.Raw("<i class=\"icon-star-empty action\"></i>")
                )</text>


    I'm trying to place an icon in the Role column of a webgrid. But my db column is an int or null so I'm getting the error : Cannot implicitly convert type 'int' to 'bool'

    Any ideas?

    Thx,
    J.Bishop

    Wednesday, September 25, 2013 4:43 PM

Answers

  • User-453926175 posted
    grid.Column(
                columnName: "Admin",
                format: @<text>@if(item.RoleId == 1) { @Html.Raw("<i class=\"icon-star action\"></i>") } else { @Html.Raw("<i class=\"icon-star-empty action\"></i>") }</text>
                ),


    Thanks for your help on this.

    This is the solution I arrived at:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 25, 2013 11:32 PM
  • User-453926175 posted
     { @Html.Raw("<a href=\"/RemoveAdmin/" + item.UserId + "\" rel=\"tooltip\" title=\"Click to remove Admin priviledge.\"><i class=\"icon-star action\"></i></a>") }


    Figured it out. Just had too many backslashes...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 26, 2013 3:45 PM

All replies

  • User-1360095595 posted

    So is RoleId a nullable int?

    Wednesday, September 25, 2013 4:47 PM
  • User-453926175 posted

    Yes. I'm using the webpages_UsersInRoles table to place users that have Admin privledges. But I've done a JOIN with my UserProfile table to return all of the users. So this is where I'm picking up the nulls.

    Wednesday, September 25, 2013 4:52 PM
  • User-1360095595 posted

    So maybe something like this:

    @(item.RoleId.HasValue && item.RoleId.Value == 5 ?
                Html.Raw("<i class=\"icon-star action\"></i>") :
                Html.Raw("<i class=\"icon-star-empty action\"></i>")

    Not sure how you want to handle nulls, so you might need to adjust some.

    Wednesday, September 25, 2013 4:57 PM
  • User-453926175 posted
    grid.Column(
                columnName: "Admin",
                format: @<text>@if(item.RoleId == 1) { @Html.Raw("<i class=\"icon-star action\"></i>") } else { @Html.Raw("<i class=\"icon-star-empty action\"></i>") }</text>
                ),


    Thanks for your help on this.

    This is the solution I arrived at:

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 25, 2013 11:32 PM
  • User-453926175 posted
    { @Html.Raw("<a href=\"/RemoveAdmin/\" + item.UserId + \" rel=\"tooltip\" title=\"Click to remove Admin priviledge.\"><i class=\"icon-star action\"></i></a>") }


    But now I need to know the proper way to encode the output to allow for dynamic information inside the Html.Raw...

    Thursday, September 26, 2013 12:13 AM
  • User-453926175 posted
     { @Html.Raw("<a href=\"/RemoveAdmin/" + item.UserId + "\" rel=\"tooltip\" title=\"Click to remove Admin priviledge.\"><i class=\"icon-star action\"></i></a>") }


    Figured it out. Just had too many backslashes...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 26, 2013 3:45 PM