Search code examples
c#asp.netasp.net-coreblazorblazorise

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);
    }
   } 

Solution

  • 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>