Search code examples
asp.net-core.net-coreasp.net-mvc-5asp.net-core-mvc

Setting up the view for user repeating code .Net Core


hey everyone i have a question. in my view i have a table that i show records in. if the record is closed the design of the table changes. right now my im doing this by @if(record closed) show <div></div> else show <div class="bg-danger"><div> the content of the two divs are identical but the second one has bg-danger there must be a way to get around this repeating content where i dont have to copy and paste the entire div for each scenario. How should i achieve that? is there a better way of setting up the view


Solution

  • You can use conditional operator ?::

    <div class="@(record closed?"":"bg-danger")"></div>
    

    Here is an official doc about conditional operator ?:.