How Convert bool to string parameter and Display Yes or No in the Field in Blazor

Here is my class that I have one boolean parameter(in Server side)

 public class ChassisDataInfo
{
    public string ChassisNo { get; set; }
    public string model { get; set; }
    public string type { get; set; }
    public bool warranty { get; set; }
}

and in client side I bind it to a TextEdit because I want to display a string to client. I mean when I put number on ChassisNumber field Warranty should fill but in string for example it must be written “It has”
          <Field>

            <TextEdit Placeholder="ChassisNumber" @bind-Text="ChassisNo"></TextEdit>

            <FieldLabel>warranty </FieldLabel>
            <TextEdit Text="@chassis.warranty.ToString()" Disabled="true"> </TextEdit>
        
         <Button Color="Color.Primary" @onclick="@SearchChassis"> <Icon Name="IconName.Search"> 
         </Icon> </Button>
         </Field>
  Code{
   ChassisDataInfo chassis = new ChassisDataInfo();
     async Task SearchChassis()
    {
        chassis = await claim.GetChassisData(ChassisNo);
    }
   }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You may either do a simple check in your view:

<TextEdit Text="@(chassis.warranty?"Yes":"No")" Disabled="true"> </TextEdit>

Or you may want to make a change in your ChassisDataInfo class and add a virtual property to it:
 public class ChassisDataInfo
{
    public string ChassisNo { get; set; }
    public string model { get; set; }
    public string type { get; set; }
    public bool warranty { get; set; }
    public virtual string hasWarranty => warranty ? "Yes" : "No";
}

and then you can use it like:
<TextEdit Text="@chassis.hasWarranty" Disabled="true"> </TextEdit>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x