How to display line under viewbag if it have null value asp.net mvc ?

Ahmed Abd El Aziz 315 Reputation points
2023-10-16T19:33:30.5966667+00:00

I work on asp.net MVC I face issue line under text not display when model property value is null or empty

i need to display line below if it exist or not 

so user will see line with no value when view bag is null 

and if it have value it will display line below 

what i try as below :

.requestInfo-flex-div-1 {
    width: 37mm;
}

 <div class="flex" style="margin-top:-10px;">

            <div class="requestInfo-flex-div-1">
                <label style="width: 20mm; border-bottom: solid 1px black; text-align: center;">@ViewBag.Solicitations </label>
            </div>
            <div style="margin-left:-100px;">
                Solicitations of Customer
            </div>

            <div style="margin-left:10px;">
                <label style="width: 20mm; border-bottom: solid 1px black; text-align: center;">@ViewBag.Restrictions</label>
            </div>
            <div style="margin-left:10px;">
                Restrictions on Solicitations of Employee
            </div>


        </div>
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 30,036 Reputation points
    2023-10-16T20:05:05.5766667+00:00

    You did not share how the ViewBag is populated. If a ViewBag property has a null value then just write an "if" condition that checks for null.

    <div class="requestInfo-flex-div-1">
        <label style="width: 20mm; border-bottom: solid 1px black; text-align: center;">@ViewBag.Solicitations </label>
    </div>
    @if (@ViewBag.Solicitations != null)
    {
        <div style="margin-left:-100px;">
            Solicitations of Customer
        </div>
    }
    

    Razor syntax reference for ASP.NET Core

    Selection statements - if, if-else, and switch


  2. QiYou-MSFT 4,326 Reputation points Microsoft External Staff
    2023-10-17T08:30:53.3833333+00:00

    Hi @Ahmed Abd El Aziz

    <style>
        .requestInfo-flex-div-1 {
            width: 37mm;
        }
    
    </style>
     <div class="flex" style="margin-top:-10px;">
    
         <div class="requestInfo-flex-div-1">
             @if (@ViewBag.Solicitations != null)
             {
                 <label style="width: 20mm; border-bottom: solid 1px black; text-align: center;">@ViewBag.Solicitations </label>
             }
             @if (@ViewBag.Solicitations == null)
             {
                 <label >@ViewBag.Solicitations </label>
             }
            
         </div>
                <div style="margin-left:10px;">
                    Solicitations of Customer
                </div>
    
                <div style="margin-left:10px;">
                    @if (@ViewBag.Restrictions != null)
                    {
                        <label style="width: 20mm; border-bottom: solid 1px black; text-align: center;">@ViewBag.Restrictions  </label>
                    }
                    @if (@ViewBag.Restrictions == null)
                    {
                        <label>@ViewBag.Restrictions s </label>
                    }
                </div>
                <div style="margin-left:10px;">
                    Restrictions on Solicitations of Employee
                </div>
    
    
            </div>
    
    
    

    The line is the style of the label. When you don't have data, don't reference style.Case3

    Best regards,
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.