Blazor Wasm Bind Values Comes With Single Quotes

Can Manalp / Egekomp 1 Reputation point
2022-09-01T14:52:09.813+00:00

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.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,406 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,401 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,294 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2022-09-13T08:34:19.197+00:00

    Hi @Can Manalp / Egekomp ,

    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

    0 comments No comments