0

I started learning C# a while ago with Tim Corey's course and while it is a bit outdated in Visual Studio version, I never had a problem similar to this.

I wrote a simple piece of code which takes a message input and shows it in a list.

Here are the two snippets of my code:

@page "/messagewall"

<h3>Message Wall</h3>
<EditForm Model="model" OnValidSubmit="ValidSubmission">
    <DataAnnotationsValidator />
    <ValidationSummary/>

    <label for="message" class="form-label">@nameof(model.Message)</label>
    <InputText id="message" @bind-Value="model.Message" class="form-control"/>

    <button class="btn btn-primary" type="submit">Submit</button>

</EditForm>

<ul>
    @foreach(string message in messages)
    {
        <li>@message</li>
    }
</ul>

@code {
    private MessageModel model = new();
    private List<string> messages = new();

    private void ValidSubmission()
    {
        messages.Add(model.Message);
    }
}
using System.ComponentModel.DataAnnotations;

namespace BlazorApp1.Models
{
    public class MessageModel
    {
        [Required]
        [StringLength(10,MinimumLength =5)]
        public string Message { get; set; }
    }
}

For some reason, whatever I do, it doesn't work if the project is a web app. I get 3 different errors.

First it was

A valid antiforgery token was not provided with the request

then I added <AntiforgeryToken /> and it worked.

After that I got another error

The POST request does not specify which form is being submitted. To fix this, ensure <form> elements have a @formname attribute with any unique value, or pass a FormName parameter if using <EditForm>

I added a form name and again it worked. But by worked I mean I was able to press submit without the getting any errors. Sadly it didn't work as intended. When I wrote anything inside the textline and click submit it should have showed the thing I wrote in a list of strings.

Instead I got another error

Text field is required

Well no matter what I did it didn't work. So I changed to web assembly and the EXACT same code worked as intended. Now what I can't understand is what are the differences between the two. How come same code piece doesn't work for the other one.

I can paste the code from the other project as well, but it is literally the same piece of code. Does web app version have some different kind of binding because most likely that is the problem since it wasn't able to recognize text from the input field.

3
  • What happens if you put @rendermode InteractiveServer at the top of the file? Commented Jul 23 at 7:49
  • Yeah that fixed the problem. I even removed "<AntiforgeryToken />" and formname aswell and it is still working. Thank you very much. But I still dont understand the reason behind it. Commented Jul 23 at 11:57
  • learn.microsoft.com/en-us/aspnet/core/blazor/components/… Commented Jul 23 at 13:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.