0

I'm working on a Blazor Server app and I have a class (User) that has a Feedback attribute. On button click I want to give a rating to my model class and calculate the feedback, but it doesn't seem to work. Also, the default value for user.Feedback is 5, so, for example if I add 4, it should be 4.5. Here's what I've done. Am I doing something wrong?

<form>
    <div class="row">
        <div class=" col-md-8">
            <div class="form-group">
                <label>Rate user:</label>
                <input type="number" id="rating" @bind-value="@rating" placeholder="rating">
            </div>
            <div class="row  my-3 px-3">
                <button class="btn btn-white ml-2" @onclick="@GiveFeedback">Give feedback</button>
            </div>
        </div>
    </div>
</form>

@code {
    [Parameter]
    public string UserId { get; set; }

    User user = new User();
    public int rating { get; set; }

    protected override async Task OnInitializedAsync()
    {
        user = await Task.Run(() => userService.GetUserAsync(Convert.ToInt32(UserId)));
    }

    protected void GiveFeedback()
    {
        user.NumberOfFeedbacks++;
        user.Feedback=(user.Feedback+rating)/user.NumberOfFeedbacks;
    }
  • 1
    `doesn't seem to work` - in which way does it not seem to work? On a side note, this is not how you [calculate a new average](https://stackoverflow.com/q/3998780/11683) based on the old average and one new data point. – GSerg Aug 31 '21 at 17:00
  • @GSerg the feedback doesn't modify – Ioana Ignat Aug 31 '21 at 17:03
  • @IoanaIgnat, they are not feedback on this UI. What do you expect? – dani herrera Aug 31 '21 at 17:39

1 Answers1

1

I think it is not a Blazor specific issue. You might have a type problem and some calculation issue as well...

  1. What is the type of user.Feedback property? Is it int or double in order to store partial (floating) number you need to use double or decimal types
  2. Average calculation also seems wrong use this line: user.Feedback=((user.Feedback * user.NumberOfFeedbacks) + rating) / user.NumberOfFeedbacks;
Major
  • 5,948
  • 2
  • 45
  • 60