question

CanManalpEgekomp-9518 avatar image
0 Votes"
CanManalpEgekomp-9518 asked ZhiLv-MSFT answered

Blazor Wasm Bind Values Comes With Single Quotes

I am trying to parse, a string property "Employee.Age" that is bonded to an input HTML tag with @bind="Employee.Age" directive. In debug its value is inside single quotes like this '37'. It must be inside double quotes like this "37" as all string values in c#. When the string value in this single quoted format, double.parse(Employee.Age) throws exception. But when it is in its correct format with double quotes it parses the string to double successfully.

dotnet-csharpdotnet-aspnet-core-blazordotnet-standard
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I am sorry but I do not understand, Hopefully everyone else does. If however you can use BlazorFiddle to provide a sample then I am sure you will get a great answer.


0 Votes 0 ·

Hi,@CanManalpEgekomp-9518 ,

I think "Employee.Age" should be an int type rather than string type,and you can try to use Double.Parse(Employee.Age+"");

0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

Hi @CanManalpEgekomp-9518,

In debug its value is inside single quotes like this '37'. It must be inside double quotes like this "37" as all string values in c#. When the string value in this single quoted format, double.parse(Employee.Age) throws exception. But when it is in its correct format with double quotes it parses the string to double

When using the Double.Parse method), the parameter should be a string that contains a number to convert. If the value is '37', it will throw a FormatException because it is a invalid format.

To prevent this exception, before parse the string, you can extract the number from the string or remove the ' character.

So, try to use the following code:

     //Age: string type. 
     Age = "'20'";
     var intnumber = System.Text.RegularExpressions.Regex.Match(Age, @"\d+").Value;
     var result = Double.Parse(intnumber);

Or, you can use the following code:

     Age = "'20'"; 
     double number;
     if (!Double.TryParse(Age, out number))
     {
         var intnumber = System.Text.RegularExpressions.Regex.Match(Age, @"\d+").Value;
         number = Double.Parse(intnumber);
     }

The result as below:

240482-image.png


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.

Best regards,
Dillion


image.png (42.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.