Is it possible to dynamically set a style attribute in a Model based on the field's value?

In order to send alerts when a specific event is about to end, I have developed a CSHTML email template file. The goal is to notify users 10 minutes before the event ends and then again at 5 minutes before the end time. To distinguish between different types of events, I would like to use color coding in the messages. Here is what I currently have:

<strong style="color: {event color}">@alert.event</strong> is scheduled to end: <strong>@endEasternTime

I am looking for a way to dynamically set the {event color} based on the value in @alert.event. I attempted to write a script within the file but struggled with transferring its return value into the style tag:

<script>
    // Retrieving the root element
    var r = document.querySelector(':root');

    // Function for determining the event color
    function getEventColor(event) {
        // Obtaining styles (properties and values) for the root
        var rs = getComputedStyle(r);

        // Assigning colors based on the event type
        return (event === "event1"
            ? rs.getPropertyValue('--evt1Color')
            : (event === "event2"
                ? rs.getPropertyValue('--evt2Color')
                : (event === "event3"
                    ? rs.getPropertyValue('--evt3Color')
                    : rs.getPropertyValue('--bdrColor')
                    )
                )
            );
    }
</script>

Please note that I have created HTML variables in the file to link color schemes with other styles present (such as border-color). For brevity and clarity, these are not included here.

Is there a viable method to accomplish this task? If not through the inline CSS approach mentioned above, could I potentially update a class or id dynamically using a technique similar to the script method discussed earlier? Any assistance provided will be greatly appreciated.

Answer №1

To create dynamic styling based on a specific event, you have a few options.

@if (@alert.event == value1) 
{     
     <strong class="color1">
}
else if (@alert.event == value2)
{     
    <strong class="color2"> 
}
else {
    <strong class="colorn">
}

Another approach is to simplify by assigning classes to events and using CSS to add colors.

<strong class="event @alert.event">

The strong tag will now have 2 classes (event and the generated value).

For example, if @alert.event = "test", apply CSS to the class like this:

.event.test {
    color: <color code>
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on creating an AngularJS application using Yesod

After successfully developing a small app using Yesod, I am now focused on adding improved interaction to it with the help of AngularJS. Currently, it appears that the AngularJS support in Yesod is still in an experimental phase. Additionally, the documen ...

iOS creates dynamic images with artifacts that appear on a generated and animated canvas

I am currently developing an HTML5 web application for WeChat, compatible with both iOS and Android devices, using only pure JavaScript without any third-party libraries like jQuery. The main feature of my app involves creating visually appealing animation ...

How can I resolve the "ReferenceError: $ is not defined" error when trying to use jQuery with Node.js on the server side?

Can someone assist me? I'm encountering an issue with using jQuery in my node.js app. Every time I attempt to use '$', I receive an error stating "$ is not defined" even though I have defined it at the beginning. Here's the process I fo ...

I'm not sure why the CSS attribute that restricts character numbers exists

https://i.sstatic.net/dkRt3.png The Divtag appears to be malfunctioning due to insufficient properties. I am currently unsure of how to resolve this issue and would appreciate any insights or opinions. <div class="col boardsBox" style="margin-right:0. ...

What is the most efficient way to use a for loop with JavaScript querySelectorAll to move multiple images?

I'm trying to move multiple images by defining each one with a for loop. Below is the code I have: var elem = document.querySelectorAll(".yikama"); var el; for (i = 0; i < elem.length; i++) { var el = elem[i] el.addEventListener(& ...

How can I automatically copy a highlighted link in HTML?

I am attempting to implement a feature where users can click on a link and have it automatically copied. For example, I want it to appear like this: "UPI ID: david@okidfcbank". In this case, the link should be highlighted in blue. This is the code I have ...

SwipeJS experiencing technical difficulties

My Swipe.Js version : "^7.0.2" Today, I attempted to use Swipe.Js and encountered an issue with my import code. import { Swiper, SwiperSlide } from 'swiper/react'; as described on https://swiperjs.com/react#installation. However, when ...

Guide on verifying internet connection status using Ajax request

Ensuring authentication by checking the availability of network connection on mobile devices. To do this, we must have both a username and password. The authentication process will be conducted online, requiring an active internet connection on the device. ...

When the user clicks, automatically direct them to a different webpage

I tried to create a changing background color effect in the following order: Black, White, Red, Green, Blue. <body style="background-color:black"> <script type="text/javascript"> function changeColor() { var currentBg ...

Having trouble including a reference to a class library project in ASP.NET 5 (Core) - looking for some

Greetings! While there have been similar questions asked in the past, my query pertains to Visual Studio 2015 RTM and ASP.NET 5 beta 6. The goal is to integrate a reference from a standard class library project into my vnext web application. Here's t ...

Guidelines for choosing AngularJS checkboxes to exclusively choose distinct matching items

Need help with selecting common items using Angular checkboxes. Below is the mock UI-Table: Order Id | Delivery Mode | Select 1 | Speed | ::checkbox:: 2 | Normal | ::checkbox:: 3 | Contractor | ::che ...

Notify customer upon database update

I'm working on developing a messaging system in asp.net. How can I notify the client when there is a change in the database? For instance, if a user's page is open and another user sends them a message, how can the software alert the user? Is it ...

Retrieving the return value from an AJAX call in C# using asynchronous methods

When it comes to retrieving a value using Ajax on the client side, I rely on this JQuery function: $.ajax({ type: "POST", url: "/my-page.aspx/checkout", contentType: "application/json; charset=utf-8", dataType: "json", success: functio ...

Unexpected JSON token error occurs in jQuery when valid input is provided

I encountered an error that I'm struggling to pinpoint. The issue seems to be related to the presence of the ' symbol in the JSON data. After thoroughly checking, I am positive that the PHP function json_encode is not responsible for adding this ...

When using jQuery's remove() or after() functions, be aware that they may inadvertently remove

Here is the html code I am working with: <p>This is some random text in a paragraph with a <span class="blue">blue</span> word.</p> <p>This is some random text in a paragraph with a <span class="blue">blue</span> ...

Alter the URL and CSS upon clicking an element

Currently, I am faced with a challenge on my website where I have multiple pages that utilize PHP to include content and jQuery to toggle the display of said content by adjusting CSS properties. However, I am encountering difficulty in implementing this ...

Pandoc fails to display SVG images when converting HTML to DOCX

I am currently utilizing pandoc for the conversion of a standalone html file (without external dependencies), incorporating all necessary css and js within the html itself. Within this HTML document, there are several svg graphs that have been generated th ...

Nested tables in Datatables retrieving child table rows based on parent table

I have been struggling for the past three days to get my nested Datatables working properly. I have a parent table called MAINtable and a child table called adjlinesTable. The issue I am facing is that all lines from the adjlinesTable are being drawn to ...

Angular's $http method is sending a GET request instead of a POST request

$http.post(main+'/api/getcard/', $.param({number: $scope.searchcard}), {headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} }) .then(function (response) { if(response.data != 0) ...

UTF-8 characters not displaying correctly in Python 3 when received through WebSocket?

I am encountering an issue with sending a JavaScript unicode star character. When interpreted by Python, the unicode characters are being displayed as the letter â, both in the console and log file. Furthermore, other unicode characters also seem to be ...