Conditional image/glyph or html based on model property value (enum)

4 posts, 0 answers
  1. Paul
    Paul avatar
    35 posts
    Member since:
    Jan 2014

    Posted 19 Oct 2015 Link to this post

    I have a model with an enum as a data type on a property.  In the grid I want to show an image representation for this enum.  Whats the best way to do this?

    I've tried using the ClientTemplate to do this.  I'm not sure what I need.  I have tried the following without success:

    @(Html.Kendo().Grid<BatchEditModel>()
            .Name("mygrid")
            .Columns(columns =>
            {
                columns.Bound(bem => bem.MyInfo.Number).ClientTemplate(
                        "# if (MyStatus == " + MyStatusConstants.Complete + ") { #
                        <span class="icon-oo-my-img-32" style="font-size: 1.2em; color: green"></span>
                        "# } #"
                        "# else if (MyStatus == " + MyStatusConstants.PWHTRequired + ") { #"
                        <span class="icon-oo-my-img-32" style="font-size: 1.2em; color: darkcyan"></span>
                        "# } #"
                        "# else if (MyStatus == " + MyStatusConstants.Myed + ") { #"
                        <span class="icon-oo-my-img-32" style="font-size: 1.2em; color: blue"></span>
                        "# } #"
                    )

    This has errors in the razor page.  I had the all the text wrapped in quotes and used + to concatenate them. 

    The question is, I have an enum as a value, but I want to display an image - which is a custom glyph icon on the span.  What is the best way to implement this?

    Thanks

    Andez

  2. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2159 posts

    Posted 21 Oct 2015 Link to this post

    Hello Paul,

    Indeed concatenating the strings is required if the template spans on multiple lines. You could find an example on the following documentation page.

    Regards,
    Dimiter Madjarov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Paul
    Paul avatar
    35 posts
    Member since:
    Jan 2014

    Posted 21 Oct 2015 in reply to Dimiter Madjarov Link to this post

    Hi Dimiter,

    That's what I was looking at.  I think issue was I was concatenating strings, but given I had an enum - enum.ToString() uses the string description - not the int.  Additionally, the "" in the string itself I wasn't sure if that was the cause.  There was a post on stack overflow that did not have concatenation.  Anyway, it is working now by making sure I had the enum integer values - and also replaced the internal double quote with single quote.

    I thought potentially it might have been the combinations of if's I had.  The demos show simple if... else's.  Might be worth making combinations more obvious on demo's. 

    Thanks

    Andez

  5. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2159 posts

    Posted 23 Oct 2015 Link to this post

    Hello Paul,

    Thanks for the update. Let us know if you experience other difficulties.

    Regards,
    Dimiter Madjarov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready